When you are running a job in unix , it occupies your terminal until
the job completes execution. So you have to wait until it completes to run
another job. To avoid such situation, you can run the job in the background and
use the terminal for other jobs/process
.
Suppose you run below command for example ;
sleep 600 --- you will have to wait 600 seconds to use the terminal for
another command/job.
Use "&" ( ampersand ) to run job in
the background
once you start , until 600 seconds (10 minutes) , your window will be
busy and you can not use the terminal until it complets(600 secs). To avoid such situation, You can run the command in the
background by adding "&"( ampersand ) at the end of the code.
sleep 600 & click ENTER
it displays below output for the instance as soon as you click ENTER :
[JobNumber] PID Number
for example:
[1] 12345
Run
in the background
To run the job in the background, use "&" at the end of the job or
command:
sleep 100 & click ENTER
You will see below information as soon
as you hit ENTER:
[2]
1234
Where "[2]" is the job number
and "1234" is the ProcessID-PID.
//OR//
A job running in the foreground ie in the terminal can be run in the background.
Let's run "sleep 1000" , first
in the foreground and then stop/pause it and resume it in the back ground.
sleep 1000 click ENTER
--- This is running job in the foreground.
To stop or pause , click
CTRL+z , the job will be stopped/paused and shows below information:
[1]+
Stopped sleep 1000
where [1] is job number ,
"Stopped" is action/status , "sleep 1000 " the job/command for which the action is
taken.
To
run the above job in the background:
Syntax : bg %JobNumber .
For the above paused job:
bg %1 click ENTER --- it displays below information
:
[1]+ sleep 1000 &
and the job is running in the
background.
See
job status
click "jobs" to see the status
as below:
Stopped ----for the stopped /paused job
Running ---- for the job running in the
background.
Done ---- for the completed job that was
running in the background.
[1]+
Stopped sleep 100
--- stopped/paused
[3]
Running sleep 50
& --- running now
[4]-
Running sleep 45
& --- in queue
Let's have an UNIX JOB like below :
Job Name : unixJob
Code :
#!/bin/bash
for i in 1 2
do
echo "Employee ID# $i "
sleep 10
done
F --- this is for producing the error
Redirect
output
Run a job in the background , redirect
the result to a file , redirect errors, if any, to the same or different output
file.
Run job or command, for example I am
running the job : unixJob
sh unixJob > file1 & --- To run
in the background and redirect the output to file1.
sh unixJob 1>file1
& --- To run in the background and redirect the output to file1.
sh unixJob 1>file1 2>&1
& --- To run in the background and redirect the output and error, if
any, both to file1.
sh unixJob 1>file1 2>file2
& --- To run in the background and redirect the output to file1 and error, if
any, to file2.
Bring the backGround job to foreGround and vice versa
Let's run sleep 1000
in the background and bring it to foreground. First of all you need to know the job number.
sleep
1000& click ENTER --- it displays below information:
[1] 11973
where [1] is the job number and
11973 is the process ID.
//or //
to know the job number of a running job in the background , ENTER "jobs" :
to know the job number of a running job in the background , ENTER "jobs" :
jobs
click ENTER --- it displays
below information:
[1] Running sleep 1000 &
now to bring this job to foreground.
Syntax:
fg
%JobNumber
fg
%1 click ENTER
Now to bring the foreground job to back ground
Run a job "sleep 1000 " in the foreground.
click CTRL+z to stop/pause the job, while doing this the job number is displayed like below:
click CTRL+z to stop/pause the job, while doing this the job number is displayed like below:
[1]+
Stopped sleep 1000
now to run the job in the background ,
bg
%1 click ENTER --- it displays below information:
[1]+ sleep 1000 &;
Let's check status with "jobs":
Let's check status with "jobs":
jobs click ENTER
--- it displays below information:
[1]+ Running sleep 1000 & --- the job is running in the background
Kill
Process/Job:
you can kill a running process/job by using "kill" command alongwith
its PID :
Let's run sleep 1000& and then kill it :
sleep 1000& click ENTER , after you click ENTER you get
below information :
[1] 9019 where [1]
is the job number and 9019 is the process ID - PID
You can also get the PID using : ps or ps -e
or ps -ef
such as ,
if you click "ps" , you get below informaiton :
PID TTY
TIME CMD
4443 pts/1 00:00:00 bash
9019
pts/1 00:00:00 sleep ---- this the job/process which we need to
kill.
9048 pts/1 00:00:00 ps
"
kill " or " kill
-9 "
Syntax :
kill PID click ENTER
kill 9019 click ENTER
now check the status of the job sleep 1000&
by
using : jobs or ps or ps -e
or ps -ef
if you check the status with the command : "jobs',
it will show " Terminated " :
[1]+ Terminated sleep 1000
if you check with the command "ps",
it will not show the job/process like below :
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
9218 pts/1 00:00:00 ps
In many situations , the job will not be terminated/killed with only "kill" command. In such
situation use "-9" with "kill".
kill -9 PID
click ENTER --- this is killing
the job/process forcefully.
kill -9 9019
click ENTER
Now, if you check the status with the command job, it shows "killed" :
[1]+ Killed sleep 1000
if you check with the command "ps",
it will not show the job/process like below :
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
9218 pts/1 00:00:00 ps
No comments:
Post a Comment