Bash: send a running process to background

2 Comments

Say you start a process and you forget to add the & character at the end of the line to send it to the background. You don’t need to restart the process if you’re using bash (and probably other shells – check the documentation of the one you’re using).

To send the running process to the background so that you get the shell prompt back to enter more commands you have to press CTRL+Z while the process is running. The process will be temporarily suspended until you send it to the foreground with the bg [jobspec] command.

Let’s see an example:

[root@localhost log]# tail -f cron
Feb 26 11:01:01 localhost CROND[5731]: (root) CMD (run-parts /etc/cron.hourly)
Feb 26 12:01:01 localhost CROND[5743]: (root) CMD (run-parts /etc/cron.hourly)
Feb 26 13:01:01 localhost CROND[5836]: (root) CMD (run-parts /etc/cron.hourly)
Feb 26 14:01:01 localhost CROND[6097]: (root) CMD (run-parts /etc/cron.hourly)
Feb 26 15:01:01 localhost CROND[6132]: (root) CMD (run-parts /etc/cron.hourly)
Feb 27 04:26:15 localhost crond[2844]: (CRON) STARTUP (1.2)
Feb 27 04:26:16 localhost crond[2844]: (CRON) INFO (running with inotify support)
Feb 27 04:26:20 localhost anacron[2912]: Anacron 2.3 started on 2009-02-27
Feb 27 04:26:20 localhost anacron[2912]: Will run job `cron.daily’ in 65 min.
Feb 27 04:26:20 localhost anacron[2912]: Jobs will be executed sequentially
^Z
[1]+  Stopped                 tail -f cron
[root@localhost log]# jobs -l
[1]+  4107 Stopped                 tail -f cron
[root@localhost log]# bg 1
[1]+ tail -f cron &
[root@localhost log]# jobs -l
[1]+  4107 Running                 tail -f cron &
[root@localhost log]# fg 1
tail -f cron
^C
[root@localhost log]# jobs -l
[root@localhost log]#

As you see when you press CTRL+Z (or ^Z) the process is suspended (stopped). You can see the list of jobs and their state by running jobs -l. This job was given the number 1. We can then send it to the background with bg 1 or bring it back to the foreground with fg 1. At the end I kill the process by pressing CTRL+C (^C).

You can stop the job with ^Z and then send it to the background using bg with no additional arguments. Actually you can use the character % as a job name. Let’s see the following example:

# tail -f test1
line 1
line 2
^Z
[1]+  Stopped                 tail -f test1
# %1 &
[1]+ tail -f test1 &
# jobs -l
[1]+  3855 Running                 tail -f test1 &
# tail -f test2
file 2 line 1
file 2 line 2
^Z
[2]+  Stopped                 tail -f test2
# %% &
[2]+ tail -f test2 &
#
# jobs -l
[1]-  3855 Running                 tail -f test1 &
[2]+  3857 Running                 tail -f test2 &
# %-
tail -f test1
^Z
[1]+  Stopped                 tail -f test1
# jobs -l
[1]+  3855 Stopped                 tail -f test1
[2]-  3857 Running                 tail -f test2 &
# kill %1

[1]+  Stopped                 tail -f test1
# kill %2
[1]+  Terminated              tail -f test1
# jobs -l
[2]+  3857 Terminated              tail -f test2
# jobs -l

The + flag on the output of jobs -l indicates the current job and the – flag indicates the previous job. The %% and %+ names refer to the current job while %- refers to the previous job. So instead of fg 1 you can use %1. And instead of bg 1 you can use %1 &. And so on…

2 Comments (+add yours?)

  1. Isaiah
    Mar 09, 2011 @ 00:55:03

    Great article! Helped me background a mysql import that a coworker had split into three parts.

  2. boucletemporelle
    Apr 12, 2011 @ 09:35:44

    Thanks a lot :) – run long simulations via ssh on several machines and this permits to continue other things after I have started them…

Leave a Reply

*