Managing Processes with job, fg, bg, screen.
Let’s talk about managing processes. If you want to run a script or scripts and then continue with other tasks while they run w/o using cron then you can use the ‘jobs’ command.
First, let create a script that will run indefinitely so we have time to work with our examples.
#!/usr/bin/python
x=0
while x == 1:
z=1
The above script will loop indefinitely since ‘x’ will never equal 1.
Next, run the script from the command line. Adding a & to the end of the script will send this jobs to the background.
jesterj@jesterj-laptop:~$ python loop.py &
[1] 4221
This sends loop.py to the background with process id ‘4221′. You can view the process id stats by doing the following:
jesterj@jesterj-laptop:~$ ps -ef | grep ‘4221′
jesterj 4221 3083 99 16:37 pts/1 00:02:40 python loop.py
To view all jobs running in the background type ‘jobs’.
jesterj@jesterj-laptop:~$ jobs
[1]+ Running python loop.py &
To add another instance of the script to the background, repeat.
jesterj@jesterj-laptop:~$ python loop.py &
[2] 4333
jesterj@jesterj-laptop:~$ jobs
[1]- Running python loop.py &
[2]+ Running python loop.py &
To quit the process use kill and the job number.
jesterj@jesterj-laptop:~$ kill %1
jesterj@jesterj-laptop:~$ jobs
[1]- Terminated python loop.py
[2]+ Running python loop.py &
To bring a process out of jobs to the foreground use, fg:
jesterj@jesterj-laptop:~$ fg %2
python loop.py