commands – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sat, 27 Aug 2022 06:59:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Run Multiple Commands in One Cron Job https://tecadmin.net/how-to-run-multiple-commands-in-one-cron-job/ https://tecadmin.net/how-to-run-multiple-commands-in-one-cron-job/#respond Sat, 27 Aug 2022 06:59:31 +0000 https://tecadmin.net/?p=31378 Crontab is a utility for running scheduled tasks at regular intervals on Unix-Linux systems. It allows us to schedule multiple cron jobs to run at once. We can schedule any shell command or script to can be executed on the terminal. Every scheduled job in crontab is separated by a new line. We can also [...]

The post How to Run Multiple Commands in One Cron Job appeared first on TecAdmin.

]]>
Crontab is a utility for running scheduled tasks at regular intervals on Unix-Linux systems. It allows us to schedule multiple cron jobs to run at once. We can schedule any shell command or script to can be executed on the terminal. Every scheduled job in crontab is separated by a new line. We can also define multiple commands or scripts in a single cron job to run one by one.

How to Sepreate Two Commands in Linux

You can separate two or more commands by using semicolons (;), logical AND (&&), or logical OR (||) operators. Which of these operators we use, is totally depends on the requirements. Here is the basic understanding of using these operators.

  1. Semicolon (;): is used to separate multiple commands. This executes all the commands without checking the exit status of previous commands.
    command_1;  command_2;  command_n
    
  2. Logical AND (&&): is used to separate commands when we want to execute the next command only if the previous command was successfully executed with exit status 0.
    command_1 &&  command_2 &&  command_n
    
  3. Logical OR (||): is used to separate commands when we want to execute the next command only if the previous command failed with a non-0 exit status.
    command_1 ||  command_2 ||  command_n
    

How to Schedule a Cron Job

First, switch to the user from which you want to run a cron job. Then open the crontab editor by running the following command.

crontab -e 

Then you add a cron job entry to the file like below:

Logical OR separated commands in Linux
Crontab commands separated with logical AND

Running Multiple Commands in Single Cron Job

Let’s discuss the real-life examples of running multiple commands with crontab with different-2 separates.

  1. Using Semicolon (;)
  2. We can separate two or more commands with semicolons, that don’t require checking the exit status of the previous command. For example, you need to change the permission of all files to 777 but need to set 777 for the logs directory.

    0 2 * * *   chmod -R 755 /var/www/myapp; chmod -R 777 /var/www/myapp/logs

    Semicolon (;) separated commands in Linux
    Crontab commands separated with semicolon (;)

  3. Using Logical AND (&&)
  4. Use this operator, where you want to run the next command only if the previous is executed successfully (exit status 0). For example, you want to run the backup.sh after successfully changing to /backup directory.

    0 2 * * *   cd /backup && bash backup.sh

    Logical OR separated commands in Linux
    Crontab commands separated with logical AND

  5. Using Logical OR (||)
  6. Use the logical OR (||) operator, when you want to run the next command only if the previous is failed (exit status non-0). For example, you want to show a message or send an email if the backup file not found.

    0 2 * * *  [ -f /backup/mydb-`date +%F`.sql ] || echo "Today's backup file not found"

    Logical AND separated commands in Linux
    Crontab commands seperated with logical AND

Conclusion

In this blog post, you have learned about running multiple commands in a single cron job entry. Also discussed various options to separate commands. The article provides you the basic details about each separator that is used to separate commands.

The post How to Run Multiple Commands in One Cron Job appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-run-multiple-commands-in-one-cron-job/feed/ 0
Running Multiple Commands At Once in Linux https://tecadmin.net/running-multiple-commands-at-once-in-linux/ https://tecadmin.net/running-multiple-commands-at-once-in-linux/#respond Sat, 06 Aug 2022 04:35:32 +0000 https://tecadmin.net/?p=30877 We’ll learn about how to execute several commands simultaneously in Linux in this article. Every operator has its own advantages when it comes to separating commands. This tutorial will help a little bit in improving how we execute commands and author shell scripts. The Linux operating system offers a simple command line interface for managing [...]

The post Running Multiple Commands At Once in Linux appeared first on TecAdmin.

]]>
We’ll learn about how to execute several commands simultaneously in Linux in this article. Every operator has its own advantages when it comes to separating commands. This tutorial will help a little bit in improving how we execute commands and author shell scripts.

The Linux operating system offers a simple command line interface for managing the system. There are shells such as Bash, CSH, and Zsh that accept commands from the user and route them to the kernel. A command is used to perform some function on the system. We may also specify multiple shells at once and execute them one after the other.

There are three distinct options available using the separator operators. In the following section, we will look at them in detail.

Operator Syntax Discription
Semicolon (;) command1; command2 Run both commands one by one
Logical AND (&&) command1 && commnd2 Run command2 only if command1 is successfull
Logical OR (||) command1 || command2 Run command2 only if commadn1 failed

Let’s discuss all the options in detail.

Using Semicolon (;)

Semicolons (;) separate commands to guarantee that subsequent commands run regardless of the previous ones’ exit statuses. Use this option to ensure that command runs after the completion of the previous one.

Syntax:

command1;  command2;  commandN

Example:

date; pwd; whoami 

Sat Aug  6 01:56:05 UTC 2022
/home/rahul
rahul

Even though the second command fails because of a permissions error, the third command still executes in the following commands:

date; touch /root/a.txt; whoami 

Sat Aug  6 01:59:31 UTC 2022
touch: cannot touch '/root/a.txt': Permission denied
rahul
Running Multiple Commands At Once in Linux with Semicolon
Running semicolon separated commands

Using Logical AND Operator (&&)

Upon successful execution of the previous command, the next command will also run. The logical AND (&&) operator checks for the exit status of the previous command.

However, if the previous command finished with non-zero exit status, the execution will stop here. No subsequent commands will run in that case

Syntax:

command1 &&  command2 && commandN

Example:

mkdir ./backups && cd ./backups 

The last command will not run if the first command failed due to any reason:

 mkdir /root/backups && cd /root/backups 

mkdir: cannot create directory ‘/root/backups’: Permission denied
Running Multiple Commands At Once in Linux with Logical AND
Using logical AND between multiple commands

Using Logical OR Operator (||)

The logical OR (||) condition checks for the exit status of the previous command and executes the next command only if the previous command failed.

Syntax:

command1 || command2 || commandN

You can use this construct in shell scripts to determine whether a file or command is available. For instance, in a backup script, you can check whether /usr/bin/mysqldump exists or not, and if not, you can print a message or terminate the process.

[ -s /usr/bin/mysqldump ] || echo "command not found" 

Use this to test command or file that isn’t on your system. This is useful for bash scripts that create files if they are missing. You can also stop script execution if required files are missing.

 [ -s /usr/bin/not_a_cmd ] || echo "command not found" 
Running Multiple Commands At Once in Linux with Logical OR
Using logical OR between multiple commands

Conclusion

In this article, we’ll go over how to run multiple commands simultaneously in Linux. We’ll also cover the various operators used to separate commands from one another. Each operator affects the way a command is executed, and each has its own benefits. This tutorial will provide some useful information to anyone interested in enhancing their command-running or shell-script-writing skills.

The post Running Multiple Commands At Once in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/running-multiple-commands-at-once-in-linux/feed/ 0