Linux commands for process creation and management system, forc, exec, bg,
fg, nohup, pkill, nice, top, ps
fork()
• A system call used to create a new child process by duplicating the parent process.
• Returns the process ID (PID) of the child to the parent and 0 to the child.
Program
import os
pid = os.fork()
if pid == 0:
print("This is the child process.")
else:
print("This is the parent process.")
exec()
• A family of system calls (execve, execl, execvp, etc.) that replace the current process image with a new one.
• Used to execute a new program within the same process ID.
Program
import os
pid = os.fork()
if pid == 0:
os.execlp("ls", "ls", "-l")
else:
print("This is the parent process.")
bg
• Resumes a suspended job in the background.
• Used after stopping a process with Ctrl + Z.
• Eg
bg %1 # Resumes job 1 in the background
fg
• Brings a background job to the foreground.
• Example
fg %1 # Brings job 1 to the foreground
nohup
• Runs a command immune to the SIGHUP signal (so it continues running even after logout).
• Example
nohup myscript.sh &
pkill
• Kills processes by name or other attributes.
• Example:
pkill firefox
nice
• Starts a process with a specified priority level.
• Lower values are higher priority (default is 0, range -20 to 19).
• Example
nice -n 10 myscript.sh # Runs with lower priority
top
• Displays real-time information about system processes and resource usage.
• Press q to exit.
• Example
Top
ps
• Displays information about active processes.
• Example
ps aux # Lists all processes with details
crontab (Manage Cron Jobs)
• The crontab command is used to create, edit, list, and remove cron jobs for a user.
Syntax:
crontab [options]
Options:
Option Description
crontab -e Edit the current user's cron jobs
crontab -l List all scheduled cron jobs
crontab -r Remove all cron jobs
crontab -u username -l List cron jobs of a specific user (requires sudo)
Example:
crontab -e # Opens the cron job editor
• Add the following line to run a backup script every day at 2 AM:
0 2 * * * /home/user/backup.sh
At command
The at command in Linux is used to schedule tasks (commands or scripts) to run at a specific time. It is useful for executing one-
time tasks in the future.
Example:
echo "echo 'Hello, World!'" | at 14:30