Question: I know how to execute a Unix command in the foreground. Can you please explain me how I can execute a Linux command in the background?
Answer: You can use one of the 5 methods explained in this article to execute a Linux command, or shell script in the background.
1. Execute a command in the background using &
You can execute a command (or shell script) as a background job by appending an ampersand to the command as shown below.
$ ./my-shell-script.sh &
Read Bg, Fg, &, Ctrl-Z – 5 Examples to Manage Unix Background Jobs to understand more details.
2. Execute a command in the background using nohup
After you execute a command (or shell script) in the background using &, if you logout from the session, the command will get killed. To avoid that, you should use nohup as shown below.
$ nohup ./my-shell-script.sh &
Read Unix Nohup: Run a Command or Shell-Script Even after You Logout to understand more details.
3. Execute a command using screen command
After you execute a command in the background using nohup and &, the command will get executed even after you logout. But, you cannot connect to the same session again to see exactly what is happening on the screen. To do that, you should use screen command.
Linux screen command offers the ability to detach a session that is running some process, and then attach it at a later time. When you reattach the session later, your terminals will be there exactly in the way you left them earlier.
Refer Screen Command Examples: Get Control of Linux / Unix Terminal to understand more details.
4. Executing a command as a batch job using at
Using at command you can schedule a job to run at a particular date and time. For example, to execute the backup script at 10 a.m tomorrow, do the following.
$ at -f backup.sh 10 am tomorrow
Read Understand at, atq, atrm, batch Commands using 9 Examples for more details.
Running certain jobs in batch mode requires certain options to be enabled. Following articles will give some clarity on those.
- 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 (Only when Passwordless login is enabled)
5. Execute a command continuously using watch
To execute a command continuously at a certain interval, use watch command as shown below.
$ watch df -h
Read Watch: Repeat Unix Commands or Shell-Scripts every N seconds to understand more details.
Comments on this entry are closed.
I wouldn’t count watch(1) as a way of executing something in the background, even though it may be a valid point, technically. Effectively, it just saves you keypresses by not having to manually re-run a foreground command/script repeatedly. Otherwise, good list, and a note of cron(8) (even though briefly) could make it even better 🙂
Always something new to learn at Geek Stuff 🙂
I could of course from the top of my head name two out of 5, (the at utility and & argument) but never got to use “watch” and “screen” commands. And I can’t remember the last time I needed to use nohup.
Great howto’s as always!
You can also use setsid command
e.g. $setsid top ; pstree
Thanks mate; you ROCK!
This one helped me.
$ nohup ./my-shell-script.sh &
wow, i learnt a new command. it’s watch. i’ve never used it before..
I like the watch command. 🙂
Hi
I want to run one command which stop/start service.
: i have stop command at /oracle/oif/oifstop.sh and start command at /oracle/oif/oifstart.sh
So once i run foo.sh then it has to do/give above both commads usage.
Please let me know ASAP.
Thanks
Vishwa
Hi Vishwa,
Even I am looking for the same pattern.
Kindly share the input.
Moreover, plz let me know how to invoke a shell script from windows (batch file).
If possible with example.
Thanks in advance.
Mk
Awesome!!