≡ Menu

5 Ways to Execute UNIX / Linux Commands (and Shell Scripts) in Background

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.

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.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 9 comments… add one }

  • dubberx December 13, 2010, 5:48 am

    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 :)

  • Gjorgi December 13, 2010, 10:49 am

    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!

  • beparas December 13, 2010, 10:12 pm

    You can also use setsid command
    e.g. $setsid top ; pstree

  • Lyes June 26, 2011, 9:15 am

    Thanks mate; you ROCK!

    This one helped me.
    $ nohup ./my-shell-script.sh &

  • robin July 8, 2011, 1:08 am

    wow, i learnt a new command. it’s watch. i’ve never used it before..

  • Jig September 1, 2011, 9:01 am

    I like the watch command. :-)

  • vishwa October 18, 2012, 3:19 am

    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.


  • MK October 18, 2012, 12:03 pm

    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.


  • CraftKung February 13, 2013, 11:32 pm


Leave a Comment