Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Processes, Services, Jobs

Process priority:
nice -n0 COMMAND # execute a command with default priority (0)
nice -n20 COMMAND # execute a command with the lowest priority (20)
nice -n-20 COMMAND # execute a command with the highest priority (-20). Only root can assign negative (high) priority
renice 10 PID # reassign priority to an existing process

List running processes and their PIDs:
echo $$ # show the PID of the current shell (or script, if invoked from a script)
ps # show all processes with a tty in current shell
ps -a # show all processes with a tty in all shells
ps -A # show all processes
ps -A | awk -F' ' '$1 == "PID"' # Show the process corresponding to PID (for example, ps -A | awk -F' ' '$1 == "1"' shows the process for PID #1, which should be systemd on Debian)
ps -A|grep "STRING" # show all processes that matches STRING
ps -A|awk '$2~/pts\// { print $0 }' # show all processes for remote terminal devices
ps -A|grep " pts/." # show all processes for remote terminal devices
pgrep -f "STRING" # show the PID for all processes that matches STRING
pgrep -fo "STRING" # show the PID for the least recent (oldest) process that matches STRING
pgrep -fn "STRING" # show the PID for the most recent (newest) process that matches STRING
ps auxf | sort -nr -k 4 # show processes sorted by memory usage
ps auxf | sort -nr -k 4 | head --lines=10 # show processes sorted by memory usage (first 10 only)
ps auxf | sort -nr -k 3 # show processes sorted by CPU usage
ps auxf | sort -nr -k 3 | head --lines=10 # show processes sorted by CPU usage (first 10 only)

Show running processes and system load:
top # Show running processes and system load. Press "q" to quit
top -o %CPU # Show running processes and system load, sorted by CPU usage. Press "q" to quit
top -o %MEM # Show running processes and system load, sorted by Memory usage. Press "q" to quit
top -b -n1 | grep COMMAND | awk '{ SUM += $10} END { print SUM }' # Show the sum of CPU usage grouped by COMMAND (for example: apache2 , mysqld , fail2ban-server )
top -b -n1 | grep COMMAND | awk '{ SUM += $9} END { print SUM }' # Show the sum of memory usage grouped by COMMAND (for example: apache2 , mysqld , fail2ban-server )

Show running processes accessing the disk or doing other Input/Output activity:
apt-get install iotop
iotop --only # press "q" to quit

Services:
service --status-all # Show the status of all services (+ for active, - for inactive)
service SERVICE status | grep -m 1 'Active:' | cut -d ":" -f2- | sed 's/^ *//g' # Show the status of a SERVICE, if it's active (running) or it's inactive. Some popular services are: ssh, apache2, tor, mysql, sendmail, fail2ban, ufw

Jobs:
CTRL+Z # Stop (Pause) current program and send it to the background. Returns Signal 20 (Exit Code 148)
jobs # List all jpbs (programs) in the background with their job id
fg JOB_ID # Restart the specified job (program) and restore it to the foreground

# If you get the message "There are stopped jobs." when logging out ('logout' from the console), run 'jobs' to see the list of jobs in the background. Then you have to options: the cleanest one is to bring the jobs to the foreground ('fg JOB_ID') and then close them as required by that specific job, or list all the running processes ('ps'), look for the one corresponding to the job stuck in the background and kill it ('kill -9 PID').  

Processes:
# LSBInitScripts require to support the following actions (as per Debian Policy, chapter 9.3.2 Writing the scripts): start, stop, restart, try-restart (optional), reload (optional), force-reload, status (optional but encouraged, see Debian Bug #291148 2005-01-19), version (not required, but useful).
/etc/init.d/COMMAND start # Start the service. Note: if the process is already running it should return success, not start another copy.
/etc/init.d/COMMAND stop # Stop the service.
/etc/init.d/COMMAND restart # Restart the service if it's already running, otherwise just report success (optional).
/etc/init.d/COMMAND try-restart # Restart the service if it's already running, otherwise just report success (optional).
/etc/init.d/COMMAND reload # Cause the configuration of the service to be reloaded without actually stopping and restarting the service (optional).
/etc/init.d/COMMAND force-reload # Cause the configuration to be reloaded if the service supports this, otherwise restart the service.
/etc/init.d/COMMAND status # Report the current status of the service (optional but encouraged, see Debian Bug #291148 2005-01-19).
/etc/init.d/COMMAND status | head -n 3 # Report if the service is loaded and active (running or exited).
/etc/init.d/COMMAND version # Return version number and / or release date (currently not required for LSB compliance).
systemctl start PROCESS # Start the service. Note: if the process is already running it should return success, not start another copy.
systemctl stop PROCESS # Stop the service.
systemctl restart PROCESS # Restart the service if it's already running.
systemctl try-restart PROCESS # Restart the service if it's already running.
systemctl status PROCESS # Report the current status of the service.
systemctl status PROCESS | head -n 3 # Show the status of the process, if it's loaded and active (running or exited)

Show which user launched a process:
fuser PID

Terminate a process:
kill -3 PID # quit a process
kill -15 PID # term a process
kill -9 PID # kill a process (most effective to quit an unresponsive process)

Terminate all processes with a matching name:
pgrep NAME | xargs kill -9
killall NAME
killall -I NAME # ignore cases
killall -i NAME # ask for confirmation before to kill
Note: If a service won't stop, kill all its PIDs. For example with Apache 2: pgrep apache2 | xargs kill -9




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 05:52 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer