1. Use the ampersand to execute commands in the background
You can add an ampersand after a Linux command or script to execute the command or script in the background, for example:.
$ ./my-shell-script.sh &
2. Use nohup to execute commands in the background
After using the & symbol to execute a command or script in the background, if you log out, the command will be automatically terminated. To avoid this situation, you can use the nohup command as follows:
$ nohup ./my-shell-script.sh &
3. Use screen to execute commands
After executing a command in the background through nohup and the & symbol, the command will continue to be executed even if you log out. However, you cannot reconnect to this session. To reconnect to this session, you can use the screen command. .
The Linux screen command provides the function of separating and reconnecting a session. When you reconnect to this session, your terminal is exactly the same as when you detached.
4. Use at to execute a command as a batch
Using the at command, you can make a command run on a specified date and time. For example, to execute a backup script in the background at 10 AM tomorrow, execute the following command:
$ at -f backup.sh 10 am tomorrow
Performing certain tasks in batch mode requires certain 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
-
How To Execute SSH and SCP in Batch Mode (Only when Passwordless login is enabled)
5. Use watch to execute a command continuously
To continuously execute a command at a fixed interval, you can use the watch command, as shown below:
$ watch df -h