User Tools

Site Tools


processes-and-jobs

Processes & Jobs


PROCESSES
pstree displays a tree of processes in alphabetical order, -n in numerical order, -p prints PID, -u prints usernames
ps displays a static list of the logged in user's running processes associated with the same terminal (PID - the process identifier, TTY - the terminal, from which the process started (character "?" means that there is no terminal associated with the process), TIME - the time during which the process was managed by the processor, CMD - the command or process and its parameters), -e all processes of all users, -f a detailed output (shows also UID - the owner of the process, PPID - the parent process identifier, C - the CPU usage, STIME - the time the process started), -l an entire output (shows also F - the process flag (0 - an ordinary process, 1 - a forked, but not executed process, 4 - a process running with root privileges, 5 - the combination of the previous values), S - the process status ("R" a running process, "S" a sleeping process, "T" a stopped process, "Z" a zombie process - its parent process terminated, "<" a high-priority process, "N" a low-priority process), C - the CPU usage in %, PRI - the priority of the process, NI - the priority value specified by "nice" command, SZ - the total memory size in blocks occupied by the process, WCHAN - the name of the kernel function in which the process waits for a particular event (running processes have "-")), -p <PID> a particular process, -C <program> processes of the particular program, -U <user / UID> processes of a particular user, -G <group / GID> processes of a particular group, -t <terminal> processes associated with a particular terminal, a all users' processes, r all running processes, u an entire and well-arranged output, x all processes, -Z prints a security context within SElinux
$ ps -ef | grep sshd
$ ps -p $$
(prints the current shell)
top displays a detailed, interactive list of all running processes in the system including CPU, memory and swap usage (the output changes according to the current state), -u <user> processes of a particular user, -p <PID> a particular process only, q quits the program
pidof <process_name> prints PID of a particular process
nice
nice (-n) <priority> <command>
prints the current process scheduling priority (standard is 0)
runs a program with a modified scheduling priority (parameter "priority" specifies a value that is going to be removed or added to the standard value, if it is omitted, the value is automaticaly decreased by 10; the priority is specified in the range between -20 and 20, the lower number - the higher priority, the minus values can be set by root only)
$ nice -15 rm -rf ~/tmp/*
(decreases the process priority by 15)
# nice --15 ls /etc | cpio -ov > /dev/rmt0
(increases the process priority by 15)
renice (-n) <priority> alters the priority of a running process (the value of the priority is specified in the absolute form), (-p) <PID> applies to a particular process, -u <user> applies to all processes of a particular user, -g <group> applies to all processes of a particular group
# renice +5 987
# renice -20 -u root
(increases the priority of all processes belonging to root to the maximum)
# renice +20 -g users
(decreases the priority of all processes belonging to group users to the minimum)
kill (<signal>) <PID> / %<job_number> terminates a particular process in a standard way (sends signal 15 - SIGTERM), -9 terminates a process immediately (signal SIGKILL), -1 terminates a process, if the signal is sent to a daemon, it rereads its configuration file (signal SIGHUP), -l prints a list of all signal names
$ kill -9 2051
$ kill -SIGTERM 2572
$ kill %3
killall (<signal>) <process_name> terminates a particular process by its name (useful especially when more running processes are associated with one program), -l prints a list of all signal names, -u <user> terminates all processes of a particular user
fuser <file / device> prints the processes using a particular file / device, -m a mounted FS, -u prints the owner of the process, -k terminates a process, -i asks for confirmation, -v detailed output
$ fuser -kimv /dev/cdrom
rpcinfo (<IP_address / hostname>) -p prints RPC services (their identification number, version, protocol, port and name) running under portmapper management on the particular or local server; the list of all RPC programs can be found in /etc/rpc


JOBS
jobs prints suspended jobs and jobs running in the background, -l including PID, -n prints the changes from the previous execution only
<command> & executes a job in the background
fg %<job_number> moves a job to the foreground
bg %<job_number> moves a job to the background (first it must be suspended by ctrl+z)
nohup <command> executes a command ignoring SIGHUP signals (it runs even if the user has logged out); the output of the command is saved in "nohup.out" file in the current directory
# nohup find /tmp -name core -print > core.txt &
watch <command> executes a command repeatedly in regular intervals of 2 seconds, -n <n> every n seconds, -d highlights the changes in the output, ctrl+c terminates the program
$ watch -d iostat
$ watch "ps -eo pcpu,pid,user,args --sort=-pcpu | head -10"
wait (<PID> / %<job_number>) waits for a specified process or job running in the background of the current shell to finish, then terminates; it returns the last process termination status, if the process doesn't exist, the return code is "127"; without the argument being specified, it waits for all the current shell background processes to terminate and the return code is "0"
$ wait $! && echo "OK" || echo "KO"
sleep <number>(<unit_of_time>) sets a time interval in seconds during which no activity is performed (useful especially when a certain delay is needed before executing the following command), m in minutes, h in hours, d in days
$ sleep 10 && echo "10 seconds have passed."
at <time> (<date>) (<additional_time_specification>) <command> executes a job at a specified time, -f <file> reads commands from a particular file, -l see command "atq", -m send a mail to the user when the job has completed
$ at 20:00 25.06.2007 / at 7am today + 3 weeks <-'
at> mail root < offer.txt <-'
at> ctrl+d
$ at -mf list 20:00 25.06.2007
atq / at -l prints a list of scheduled jobs (a job number, time of execution, a user who scheduled the job)
atrm / at -d <job_number> cancels a scheduled job
lpr <file> prints a file, -P <printer_name> specifies a printer, -# <n> specifies the number of copies
lpq shows the current printer queue status, -a jobs on all printers, -l detailed output
lprm <job_number> / - cancels a particular print job that have been queued for printing / cancels all print jobs
crontab (<file>) sets up an existing file for periodical execution of particular jobs via "cron" daemon, -e creates or edits a crontab file, -l prints the contents of the crontab file, -r removes a crontab file, -u <user> sets up a crontab file of the particular user (available for root only); the jobs are specified each on a separate line in the file which in the users' files consists of 6 fields separated by blank spaces - 5 for time specification (in the order of a minute, hour, day in the month, month and day in the week (Su - Sa = 0 - 6), "*" equals to all values, "*/"<n> every nth time interval, "," separates values of the same field, "-" indicates a range of values) and 1 for the command itself (the command must be specified in the absolute form, except for the shell builtin commands and keywords, if it gives an output, it is sent to the user by a mail); an empty line and line beginning with "#" character is ignored; the system file is /etc/crontab and files situated in /etc/cron.d, which contain 1 extra field between the time specification and the command - the user under which the job runs; users' files are stored in /var/spool/cron/<user> and if they are edited manually (not by "crontab -e" command), the changes are not atomatically re-read by the daemon; authorized users are specified in /etc/cron.allow, unauthorized users in /etc/cron.deny, if both the files exist, /etc/cron.deny is ignored
30 8 * * * /usr/bin/df | /bin/mail admin
(executes a job every day at 8:30)
*/30 8-16 * * 1-5 /usr/bin/who >> /tmp/users.out
(executes a job every 30 minutes in the working hours)
0 22 * * 5 /usr/local/scripts/backup.sh
(runs a script every Friday at 22:00)
0 0 1 * * for file in $(/bin/find /web/logs/*); do > $file; done
(removes particular logs at the beginning of every month)
Last modified: 2017/06/29 11:08 by Miroslav Bernát

visits: