commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
The classical 'ps aux | grep' can do this with one more parameter, the '-v', with means 'NOT' to grep.
Hides the process "your_command" from showing with ps, displaying some other random process name already running for a better camouflage.
Will open strace on all apache process, on systems using sbin/apache (debian) or sbin/httpd (redhat), and will follow threads newly created.
This command will add up RAM usage of all processes whose name contains "java" and output the sum of percentages in HRF. Also, unlike the original #15430, it wont fail on processes with a usage of >9.9%.
Pleases note that this command wont work reliably in use cases where a significant portion of processes involved are using less than 0.1% of RAM, because they will be counted as "0", even though a great number of them could add up to significant amounts.
Note that `grep "$(ir foo)"` really doesn't save any typing, but wrapping this inside a second shell function will:
psg() { grep "$(ir \"[email protected]\")" ;}
ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.
David thanks for that grep inside!
here is mine version:
psgrep()
{
case ${1} in
( -E | -e )
local EXTENDED_REGEXP=1
shift 1
;;
*)
local EXTENDED_REGEXP=0
;;
esac
if [[ -z ${*} ]]
then
echo "psgrep - grep for process(es) by keyword" >&2
echo "Usage: psgrep [-E|-e] ... " >&2
echo "" >&2
echo "option [-E|-e] enables full extended regexp support" >&2
echo "without [-E|-e] plain strings are looked for" >&2
return 1
fi
\ps -eo 'user,pid,pcpu,command' w | head -n1
local ARG=''
if (( ${EXTENDED_REGEXP} == 0 ))
then
while (( ${#} > 0 ))
do
ARG="${1}"
shift 1
local STRING=${ARG}
local LENGTH=$(expr length ${STRING})
local FIRSCHAR=$(echo $(expr substr ${STRING} 1 1))
local REST=$(echo $(expr substr ${STRING} 2 ${LENGTH}))
\ps -eo 'user,pid,pcpu,command' w | grep "[${FIRSCHAR}]${REST}"
done
else
\ps -eo 'user,pid,pcpu,command' w | grep -iE "(${*})"
fi
}
Function that searchs for process by its name:
* Shows the Header for reference
* Hides the process 'grep' from the list
* Case sensitive
The typical problem with using "ps | grep" is that the grep process shows up the in the output.
The usual solution is to search for "[p]attern" instead of "pattern".
This function turns the parameter into just such a [p]attern.
${1:0:1} is the first character of $1
.
${1:1} is characters 2-end of $1
Function that searchs a process by its name and shows in the terminal.
* Shows the Header for reference
* Hides the process 'grep' from the list
* Case sensitive
This checks the system load every second and if it's over a certain threshold (.8 in this example), it spits out the date, system loads and top 4 processes sorted by CPU.
Additionally, the \a in the first echo creates an audible bell.
Given a hosts list, ssh one by one and echo its name only if 'processname' is not running.
If you want to see your top ten cpu using processes from the browser (e.g. you don't want to ssh into your server all the time for checking system load) you can run this command and browse to the machines ip on port 8888. For example 192.168.0.100:8888
If you have ever been trying to look for a list of processes based on their elapsed time you don't need to look any further.
This command lets you find the list of processes ordered in a reversed order (oldest at the top) that have been running for over an hour on your system. Any system processes are filtered out, leaving only user initiated ones in. I find it extremely useful for debugging and performance analysis.
Llist all the processes in the run queue.
Pipes the header row of ps to STDERR, then greps for the command on the output of ps, removing the grep entry before that.
This version also attaches to new processes forked by the parent apache process. That way you can trace all current and *future* apache processes.
Finding high memory usage report in human readable format.
Using the output of 'ps' to determine CPU usage is misleading, as the CPU column in 'ps' shows CPU usage per process over the entire lifetime of the process. In order to get *current* CPU usage (without scraping a top screen) you need to pull some numbers from /proc/stat. Here, we take two readings, once second apart, determine how much IDLE time was spent across all CPUs, divide by the number of CPUs, and then subtract from 100 to get non-idle time.
top accecpts a comma separated list of PIDs.