Bootstrap FreeKB - Linux Fundamentals - Running jobs in the foreground and background in Linux
Linux Fundamentals - Running jobs in the foreground and background in Linux

Updated:   |  Linux Fundamentals articles

Let's use the ps command to view the processes running in our shell. Two processes are running, bash and the ps command.

~]#  ps
PID    TTY     TIME       CMD
15415  pts/0   00:00:00   bash
15435  pts/0   00:00:00   ps

 

Let's say you have a bash shell script named sleep.sh, which will run in the foreground for 60 seconds.

#!/bin/bash
sleep 60

 

Let's invoke sleep.sh in the foreground.

~]# bash sleep.sh

 

If we open another terminal, we can see that there is a PID for sleep.sh.

~]# ps -ef | grep vi
john.doe 61208  60996  0 07:57 pts/0    00:00:00 bash sleep.sh

 

There are a few ways to run sleep.sh in the background.

One option is to use the & character.

~]# bash sleep.sh &

 

Another option is to use the nohup (no hang up) command.

~]# nohup bash sleep.sh

 

If you didn't use the & character or the nohup (no hang up) command, you can use ctrl + Z to create a job. In this example [1] means job %1.

[1]+  Stopped                 bash sleep.sh

 

You can then use the bg command to resume sleep.sh in the background.

~]$ bg %1
[1]+ bash sleep.sh &

 

The fg command can be used if you want to move it back into the foreground.

~]# fg %1

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ce1af0 in the box below so that we can be sure you are a human.