- How to Run Multiple Linux Commands at Once in Linux Terminal [Essential Beginners Tip]
- Using ; to run multiple Linux commands in one line
- Using && to run multiple Linux commands
- Using || to run several Linux commands at once
- Bonus Tip: Combine && and || operators
- Running multiple commands in one line in shell
- 6 Answers 6
- Multiple commands on a single line in Linux [duplicate]
- 2 Answers 2
- Execute combine multiple Linux commands in one line
- 11 Answers 11
- How To Run Multiple Commands In Parallel on Linux
- Running Commands in Parallel using Bash Shell
- How to Run Multiple Processes Simultaneously using Xargs?
- How to Use GNU Parallel to Run commands simultaneously?
- How to Manage Output in GNU Parallel?
- Parallel Execution of Commands on a list of Remote Machines
How to Run Multiple Linux Commands at Once in Linux Terminal [Essential Beginners Tip]
Last updated September 15, 2020 By Abhishek Prakash 35 Comments
Running two or more commands in one line can save you a good deal of time and help you become more efficient and productive in Linux.
There are three ways you can run multiple commands in one line in Linux:
; | Command 1 ; Command 2 | Run command 1 first and then command 2 |
&& | Command 1 && Command 2 | Run command 2 only if command 1 ends sucessfully |
|| | Command 1 || Command 2 | Run command 2 only if command 1 fails |
Let me show you in detail how you can chain commands in Linux.
Using ; to run multiple Linux commands in one line
The simplest of them all is the semicolon (;). You just combine several commands that you want to run using ; in the following fashion:
Here, cmd1 will run first. Irrespective of whether cmd1 runs successfully or with error, cmd2 will run after it. And when cmd2 command finishes, cmd3 will run.
Let’s take an example you can practice easily (if you want to).
In the above command, you first create a new directory named new_dir with mkdir command. Then you switch to this newly created directory using cd command. Lastly you print your current location with pwd command.
The space after semicolon (;) is optional but it makes the chain of commands easily readable.
Using && to run multiple Linux commands
Some times you want to ensure that the in the chain of Linux commands, the next command only runs when the previous command ends successfully. This is where the logical AND operator && comes into picture:
If you use Ubuntu or Debian based distributions, you must have come across this command that utilizes && concept:
Here the first command (sudo apt update) first refreshes the package database cache. If there is no error, it will then upgrade all the packages that have newer versions available.
Let’s take earlier example. If the new_dir already exists, mkdir command will return error. The difference in the behavior of ; and && can be see in the screenshot below:
Did you see how commands separated by && stopped when the first command resulted into error?
Using || to run several Linux commands at once
You can use the logical OR operator (||) to run a chain of commands but the next command only runs when the previous command ends in error. This is opposite to what you saw with &&.
If cmd1 fails, cmd2 runs. If cmd2 runs successfully, cmd3 won’t run.
In the screenshot above, mkdir new_dir command fails because new_dir already exists. Since this command fails, the next command cd new_dir is executed successfully. And now that this command has run successfully, the next command pwd won’t run.
Bonus Tip: Combine && and || operators
You may combine the operators to run two or more Linux commands.
For example, you can check if file exists in bash, and print messages accordingly.
Run the above command before and after creating the file.txt file to see the difference:
Like copy-paste in Linux terminal, running multiple commands at once is also one of the many Linux command line tips for saving time. Though elementary, it is an essential concept any Linux terminal user should know.
You can also use ;, && and || to run multiple commands in bash scripts as well.
I hope you liked this terminal trick. Stay tuned for more Linux command tips and tools published every Tuesday under the #TerminalTuesday series.
Like what you read? Please share it with others.
Источник
Running multiple commands in one line in shell
Say I have a file /templates/apple and I want to
- put it in two different places and then
- remove the original.
So, /templates/apple will be copied to /templates/used AND /templates/inuse and then after that I’d like to remove the original.
Is cp the best way to do this, followed by rm ? Or is there a better way?
I want to do it all in one line so I’m thinking it would look something like:
Is this the correct syntax?
6 Answers 6
You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:
To summarize (non-exhaustively) bash’s command operators/separators:
- | pipes (pipelines) the standard output ( stdout ) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
- |& pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
- && executes the right-hand command of && only if the previous one succeeded.
- || executes the right-hand command of || only it the previous one failed.
- ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.
Источник
Multiple commands on a single line in Linux [duplicate]
I’d like to run several commands on the command line.
In a normal case this is simple:
However when one of the commands ends itself on a & this doesn’t seem to work:
I’ve tried without the single trailing & but this obviously halts the processing of the latter echo . Just for fun tried a triple & but this also returns an error.
So my question; how can I get
executed on one single line?
2 Answers 2
First, if you want to run multiple commands in one line, separate them by a ; :
The && is the logical and operator. If you issue
cmd2 will only run if cmd1 succeeded. That’s important to mention (also see below).
The & is not a logical operator in this case, it tells bash to run cmd1 in background.
In your case, the commandline needs syntactically look like this:
However, I guess you really meant this:
because otherwise you would not be able to start the process if it is not already running, since killall would return a non zero return code.
Even having this the code is quite fragile. I suggest to use your operating systems facilities to start vsftp as a daemon. I mean facilities like the command start-stop-daemon .
You can encapsulate commands (or sequences of commands) in parentheses, like so:
However, this doesn’t make much sense semantically as the && token means “run subsequent command if previous command successful” (i.e., return value of 0), and the single & puts the command preceding it into the background (continuing immediately with the next command), which always yields success.
In your case, there really isn’t any way to determine the success of running the vsftpd command when it is backgrounded, unless the executable offers command-line arguments running the thing as a daemon so you needn’t background it manually.
Источник
Execute combine multiple Linux commands in one line
I am trying to merge multiple linux commands in one line to perform deployment operation. For example
11 Answers 11
If you want to execute each command only if the previous one succeeded, then combine them using the && operator:
If one of the commands fails, then all other commands following it won’t be executed.
If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons:
In your case, I think you want the first case where execution of the next command depends on the success of the previous one.
You can also put all commands in a script and execute that instead:
(The backslashes at the end of the line are there to prevent the shell from thinking that the next line is a new command; if you omit the backslashes, you would need to write the whole command in a single line.)
Save that to a file, for example myscript , and make it executable:
You can now execute that script like other programs on the machine. But if you don’t place it inside a directory listed in your PATH environment variable (for example /usr/local/bin , or on some Linux distributions
/bin ), then you will need to specify the path to that script. If it’s in the current directory, you execute it with:
The commands in the script work the same way as the commands in the first example; the next command only executes if the previous one succeeded. For unconditional execution of all commands, simply list each command on its own line:
Источник
How To Run Multiple Commands In Parallel on Linux
Traditionally computers can only do one single thing at a time. It generally does not do multitasking. Well, in most cases (with single core computers — meaning computers with one single CPU), the computer gives you an illusion that multiple things are happening simultaneously.
You might be running multiple things at the same time on a computer, but the computer will always execute one single task from your list of tasks at a time. But it will quickly switch to the next task, and then the next task and then the next (so basically in a way you can say that multiple tasks are progressing one by one).
This switching between tasks happens so fast that it is very difficult for us to notice. A typical computer will do 100s of switching between tasks in a single second. You can now imagine why we are under the illusion that multiple tasks are being executed at the same time.
This switching is generally called as context switching. When you have too many number of tasks waiting in line for CPU, then we say that the “machine is under load”. In this article, we are going to discuss the methods available to execute multiple processes in parallel on a Linux system.
The best example is to execute a command across 10s of servers from a machine. If you go one by one, it will consume a lot of time. But if we have a method to run the command simultaneously across all the servers in parallel, then that saves a lot of time. Again, from a computer/CPU standpoint it mainly deals with one task at a time, but keeps on switching between tasks, which happens too fast, and we are perfectly fine as far as multiple tasks are progressing simultaneously.
Let us dig through the different methods that are available in Linux, to execute commands simultaneously(in Parallel). The very first one is using the bash shell job control mechanism. We simply execute the command and then ask the shell to place that in background and proceed with the next command(while the first command is already being executed in the background), and then the next and so on.
To demonstrate this, I have few junk files sitting in an object storage. Downloading all these files to a Linux machine can be done simultaneously to test and see how this parallel thing works.
Running Commands in Parallel using Bash Shell
I basically need to execute 3 wget commands and make them run in parallel. The best method is to put all the wget commands in one script, and execute the script. The only thing to note here is to put all these wget commands in background (shell background). See our simple script file below.
Notice the & towards the end of each command. This will put the command in background, and execute the next (put this in background) and proceed to the next and so on.
You can confirm all these commands are being executed simultaneously using another shell, and see the process list (in our case it should show 3 wget commands, with three different processes).
We can clearly see from the above output that our three wget commands are running in parallel.
In case you need to execute several processes in batches, or in chunks, you can use the shell builtin command called «wait». See below.
The first three commands wget commands will be executed in parallel. «wait» will make the script wait till those 3 gets finished. Once it is finished, the script will simultaneously run the next 6 commands, and wait till it completes and so on.
We can modify our script and make it a bit more generic as shown below.
Now you can run as many commands as you like by using the script as shown below.
How to Run Multiple Processes Simultaneously using Xargs?
The next method that we can use to run processes in parallel is our regular xargs command. Xargs supports an option to specify the number of processes that you want to run simultaneously. See below.
seq command will simply give 1, 2, and 3 as output in three lines. Which is then passed to xargs as input using the standard Linux pipe. We have used the option -I in xargs to remove the space character that generally gets added towards the end of our command.
Without -I<> and the last junkfile<>, xargs would construct our command with the file name of «junkfile 1, junkfile 2 etc» (we needed to remove the space), rather than junkfile1, junkfile2 and so on.
You can quickly confirm that 3 processes are running in parallel (as we passed -P 3), using another terminal and counting the number of wget processes as we did earlier. See below.
wget is just an example that we are using here for our tutorial. One reason for using wget with those three junkfile is to keep the process alive for few minutes, so that we can confirm the parallel execution (as the files are quite big, these processes will couple of few minutes to finish). You can replace wget with whatever is applicable in your use case.
How to Use GNU Parallel to Run commands simultaneously?
Apart from this, there is a tool from GNU, which is designed to execute jobs in parallel. Its called GNU Parallel. It can be installed by the below commands (depending upon your Linux distribution).
For RedHat Based Systems:
For Debian Based Systems(Ubuntu):
You can use GNU Parallel for some of the below use cases. These use cases nicely covers regular shell based system admin activities.
- A list of files that can be passed as input to parallel command, to do some operations in parallel on all of them
- You can give a list of IP addresses/hostnames, on which you need to fire up a command in parallel
- List of links/URLs (similar to our wget example we saw with xargs and shell above)
GNU Parallel was designed by keeping xargs in mind, so majority of the command line options and parameters might match with xargs command. Let’s first execute the wget example that we saw using GNU parallel.
If you see the above command, parallel is using -j option (very similar to xargs, to specify the number of processes to run in parallel), and -I option (to remove the default space character). And without a doubt, it will run all our 3 wget commands simultaneously.
Do you want to compress all files in the current directory (in parallel and simultaneously)? We can do that very easily with parallel. Below shown is an example to achieve just that.
In the above example, a maximum of 10 compression will happen together. Similarly to uncompress/gunzip all the files simultaneously, run the below.
You can gzip all the files in the current directory using the below method as well (in parallel). In our below example, we have limited the number of jobs to 10, that will run in parallel.
Parallel has the option to pass a file as an argument, so that it can run command against entries in the file. For example, you can have a file with a list of URLs to download.
The above should download the URLs listed in the file «list-of-urls.txt» in parallel.
Parallel can also execute a series of commands specified in a test file. See an example below. Lets first create a test file with few sleep commands in there.
Now let us ask parallel to execute all the commands in that file simultaneously. This can be done as shown below.
How to Manage Output in GNU Parallel?
A common issue while executing multiple commands in parallel is output. The output of different commands should not get mixed up. If you use the very first method that we saw in this article (ie: using the shell job control mechanism), there is actually no guarantee of the order of the output. For example, let us try ping command towards multiple hosts using the shell method of &.
You can clearly see the output is completely messed up(outputs of those three pings are mixed up). Now let’s try with parallel, and prevent the output from getting messed up. See below.
Basically parallel will show the complete output of one process, only when it completes. Does the order of the output matter to you? If you want the output also to be in the same order as your input, then you can use -k option in parallel as shown below.
Please keep the fact in mind that GNU Parallel will find out the number of CPU cores available in the system, and it will run only one job per core. We used -j option in our examples to override this default behavior. If you have 100 commands to execute using GNU parallel, the jobs will be executed in smaller chunks. The chunk size is again determined by -j option.
If you want to quickly terminate GNU parallel, you can run fire up the below command. On receiving this signal, GNU parallel will finish the currently executing chunk and exit.
Parallel Execution of Commands on a list of Remote Machines
To be honest, I have not found GNU Parallel that user friendly, when it comes to remote command execution on a list of servers simultaneously. For this purpose, there are tools like clustershell and pdsh (agreed that GNU parallel has parameters like sshlogin and sshloginfile), but i did not find that straight forward. In my test cases, some of these commands were not stable enough to recommend to execute against number of servers in parallel.
Am going to start this with clustershell and then pdsh.
Clustershell can be easily installed using the command applicable to your platform.
RedHat Based Systems:
Ubuntu Based Systems:
As the name indicates, it is used for administering a cluster of servers. Basically execute something/fetch information from all machines in a cluster. Similar to other Linux utilities, the configuration file for clustershell is located at /etc/clustershell/clush.conf. I have the below inside that file.
You can execute a command of your interest across a coma separated list of servers using clustershell as below.
Please note the fact in mind that clush uses the SSH private key inside /home/$user/.ssh/id_rsa file. For example, if am executing this command as «ubuntu» user, then the private key used will be /home/ubuntu/.ssh/id_rsa. The corresponding public key is expected to be present on all the servers where you are executing the command using clustershell.
You can use shortcuts, and regular expressions, if you have servers in the format of node1.example.com or something like that. See below.
You can copy files in parallel to multiple servers using clsutershell using the —copy option.
The above command will copy the file /home/ubuntu/testfile to the same location on all servers
You can also creates a grouping of servers using the file /etc/clustershell/groups (if the file does not exist, then create it). An example groups file is shown below.
You can now execute commands against these groups by calling the group name (web and db in our case).
Clustershell supports an interactive mode for executing commands across multiple machines. It is quite interesting. We simply pass the -b option to clush command against one of our group and we can interactively fire commands on these group. An example is below.
Similar to clustershell is another utility named pdsh. The idea for pdsh is similar to the utility rsh, which can be used to execute something on one remote host. However, pdsh can be used to execute commands in parallel on multiple hosts.
Similar to clustershell, the installation is quite straightforward (a single apt-get or yum command depending upon your distribution).
RedHat Based Systems:
Debian/Ubuntu Based Systems:
The very first thing to do is to tell pdsh that we would like to use SSH for remote connections. This can be done using an environment variable.
To make it permanent, you can add it inside the user’s .bashrc file as well. After executing the below command, you can logout and login to confirm that the environment variable is set and available for the user.
Let us fire up a command against our node2.example.com using pdsh. See below.
Without specifying the RCMD environment variable, you can also run commands like the one shown below.
Another interesting environment variable that we can use with pdsh is WCOLL. This will specify a file which contains a list of servers.
Similar to our previous example, you can add this one to .bashrc to make it permanent.
Источник