1. Use the & symbol to execute commands in the background
You can add the & symbol to the Linux command or script to make the command or script execute in the background, for example:.
$./my-shell-script.sh &
2. Use Nohup to execute commands in the background
After you use the & symbol to execute commands or scripts in the background, the command is automatically terminated if you log out. To avoid this situation, you can use the Nohup command as follows:
$ nohup./my-shell-script.sh &
3. Use screen to execute commands
After the command is executed in the background through the Nohup and & symbols, the command will be executed even if you log out. However, you cannot reconnect to this session and you can use the screen command to reconnect to this session.
The Linux Screen command provides the ability to detach and reconnect a session. When you reconnect to this session, your terminal is exactly the same as when you were separated.
4. Use at to execute a command as a batch
With the AT command, you can have a command run at a specified date and time, for example, to execute the backup script in the background at 10 o'clock in the morning tomorrow, and execute the following command:
$ at-f backup.sh tomorrow
Some of the tasks that you perform in batch mode require some options to be enabled. The following article will give a detailed explanation:.
- How-to-Capture Unix Top Command Output to a File in readable Format
- Unix BC Command Line Calculator in Batch Mode
- How-to-Execute SSH and SCP in Batch Mode (if passwordless login is enabled)
5. Use watch to execute a command continuously
To run a command at a constant interval, you can use the Watch command as follows:
$ watch Df-h
Shell script timed background execution