Processes in Linux2
Processes in Linux2
Foreground Processes
By default, every process that you start runs in the foreground. It gets its input from the keyboard
and sends its output to the screen.
$ ls ch*.doc
While a program is running in the foreground and is time-consuming, no other commands can be
run (start any other processes) because the prompt would not be available until the program
finishes processing and comes out.
Background Processes
A background process runs without being connected to your keyboard. If the background process
requires any keyboard input, it waits.
The advantage of running a process in the background is that you can run other commands; you
do not have to wait until it completes to start another!
The simplest way to start a background process is to add an ampersand (&) at the end of the
command.
$ps
PID TTY TIME CMD
18358 ttyp3 00:00:00 sh
18361 ttyp3 00:01:31 abiword
18789 ttyp3 00:00:00 ps
One of the most commonly used flags for ps is the -f ( f for full) option, which provides more
information
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts/6 0:00 ps -f
UID
1
User ID that this process belongs to (the person running it)
PID
2
Process ID
PPID
3
Parent process ID (the ID of the process that started it)
C
4
CPU utilization of process
STIME
5
Process start time
TTY
6
Terminal type associated with the process
TIME
7
CPU time taken by the process
CMD
8
The command that started this process
Stopping Processes
Ending a process can be done in several different ways. Often, from a console-based command,
sending a CTRL + C keystroke (the default interrupt character) will exit the command
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts/6 0:00 ps -f
$kill 6738
Terminated
kill command terminates the first_one process. If a process ignores a regular kill command, you
can use kill -9 followed by the process ID as follows –
$kill -9 6738
Terminated
Top
It is an interactive diagnostic tool that updates frequently and shows information about physical
and virtual memory, CPU usage, load averages, and your busy processes.
top - 16:56:36 up 2 days, 22:30, 0 users, load average: 0.83, 1.16, 1.11
%Cpu(s): 8.1 us, 2.9 sy, 0.9 ni, 83.6 id, 2.7 wa, 0.0 hi, 1.8 si, 0.0 st
MiB Mem : 32090.3 total, 1267.8 free, 5300.5 used, 25522.0 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 24699.8 avail Mem
Normally, when a child process is killed, the parent process is updated via a SIGCHLD signal.
Then the parent can do some other task or restart a new child as needed. However, sometimes the
parent process is killed before its child is killed. In this case, the "parent of all processes,"
the init process, becomes the new PPID (parent process ID). In some cases, these processes are
called orphan processes.
The ping command sends an echo request to a host available on the network. Using this
command, you can check if your remote host is responding well or not.
Syntax
Here, ftp stands for File Transfer Protocol. This utility helps you upload and download your file
from one computer to another computer.The ftp utility has its own set of Unix-like commands.
These commands help you perform tasks such as −
put filename
1 Uploads filename from the local machine to the remote
machine.
get filename
2 Downloads filename from the remote machine to the local
machine.
prompt off
5 Turns the prompt off. By default, you will receive a prompt to
upload or download files using mput or mget commands.
prompt on
6
Turns the prompt on.
Dir
7 Lists all the files available in the current directory of the
remote machine.
cd dirname
8
Changes directory to dirname on the remote machine.
lcd dirname
9
Changes directory to dirname on the local machine.
Quit
10
Helps logout from the current login.
The telnet Utility
There are times when we are required to connect to a remote Unix machine and work
on that machine remotely. Telnet is a utility that allows a computer user at one site to
make a connection, login and then conduct work on a computer at another site.
Once you login using Telnet, you can perform all the activities on your remotely
connected machine. The following is an example of Telnet session −
C:>telnet amrood.com
Trying...
Connected to amrood.com.
Escape character is '^]'.
login: amrood
amrood's Password:
*****************************************************
* *
* *
* WELCOME TO AMROOD.COM *
* *
* *
*****************************************************
{ do your work }
$ logout
Connection closed.
C:>
$ finger amrood
Login: amrood Name: (null)
Directory: /home/amrood Shell: /bin/bash
On since Thu Jun 25 08:03 (MST) on pts/0 from 62.61.164.115
No mail.
No Plan.
$ finger @avtar.com
Login Name Tty Idle Login Time Office
amrood pts/0 Jun 25 08:03 (62.61.164.115)
$ finger amrood@avtar.com
Login: amrood Name: (null)
Directory: /home/amrood Shell: /bin/bash
On since Thu Jun 25 08:03 (MST) on pts/0 from 62.61.164.115
No mail.
No Plan.
Task Scheduling:
1. at Command:
• Description: The at command schedules a one-time task to run at a specific time in the future.
• Usage:
• at: Schedule a task to be executed once at a specific time.
• atq: List the pending at jobs for the current user.
• atrm: Remove a pending at job.
• Syntax:
• at <time>: Schedules a new job to run at the specified time.
• Example: at now + 1 hour
2. batch Command:
• Description: The batch command schedules tasks to run when system load levels permit.
• Usage:
• batch: Schedule a job to be executed when system load levels drop below a certain threshold.
• atq: List the pending batch jobs for the current user.
• atrm: Remove a pending batch job.
• Syntax:
• batch: Schedules a job to be executed when the system load is low.
• Example: batch
3. cron:
• Description: cron is a time-based job scheduler that runs recurring tasks at predefined intervals.
• Files:
• /etc/crontab: System-wide cron jobs configuration file.
• /etc/cron.d/: Directory for system-wide cron job files.
• /var/spool/cron/crontabs/ : Directory for individual user cron jobs files.
• Commands:
• crontab -e: Edit the current user's crontab file.
• crontab -l: List the current user's cron jobs.
• crontab -r: Remove the current user's cron jobs.
• Cron Syntax:
• Minute (0-59), Hour (0-23), Day of Month (1-31), Month (1-12), Day of Week (0-7 where 0 and 7
represent Sunday).
• * indicates all possible values.
• Example: 0 3 * * * /path/to/command (Runs a command at 3:00 AM daily).
Examples:
1. Schedule a job to run a script at 2:30 PM:
crontab -e 30 4 * * * /path/to/command