With commands such as bg fg jobs, we can do something about jobs in the background.

&

Keep a command running in the background of the current session:

1
vi &
img

bg

Wake up the paused background processes in the current environment and continue running.

1
bg
img

fg

Moves a background process from the current environment to the foreground.

1
fg
img

jobs

Show the status of the background jobs in the current session.

Here are some options for jobs command:

Option Description
-l Show the job number, current job, process group number, status, and the command that make up the job.
-p Show the process group leader IDs of the jobs.
1
2
3
vi &
jobs -l
jobs -p
img

nohup

Prepare an infinite loop script:

1
2
3
4
5
printf '%s\n' \
'#!/bin/bash' \
'while true; do' \
'echo "a"' > script.sh
echo 'done' >> script.sh

Keep the script running in the background, even if the user logs out of the system, the running state of the command will not be terminated.

1
2
3
4
5
6
7
# echo endless a characters to script.out file
nohup sh script.sh > script.out &
ll script.out
ll script.out
ll script.out
ll script.out
ll script.out
img

Check for running jobs’ process IDs:

1
jobs -l
img

Kill the running process:

1
kill 9802
img

Double check the jobs:

1
jobs -l
img

References 7.2 Job Control Builtins, BG(1P), FG(1P), NOHUP(1), JOBS(1P)

Buy me a coffeeBuy me a coffee