Process

Updated: 2018-11-30
  • start a process

    • foreground
    • background
  • stop a process
  • kill a process
  • schedule a process

pstree

$ pstree
init─┬─acpid
     ├─apache2───2*[apache2───26*[{apache2}]]
     ├─atd
     ├─cron
     ...

which vs whereis

  • which: based on PATH

    $ which hadoop
    /Users/myname/lib/hadoop
  • whereis: based on standard binary directories

    $ whereis hadoop
    hadoop: /usr/bin/hadoop /etc/hadoop /usr/lib/hadoop /usr/share/man/man1/hadoop.1.gz

Check If Command Exists

$ command -v foo >/dev/null 2>&1
$ type foo >/dev/null 2>&1
$ hash foo 2>/dev/null

Example: add HADOOP_CLASSPATH if hadoop exists

command -v hadoop > /dev/null && {
    HADOOP_CLASSPATH=`hadoop classpath`
    CLASSPATH=$HADOOP_CLASSPATH:$CLASSPATH
}

Get PID

$ pgrep mongod

Multiple programs under the same name:

$ ps -eo pid,args | grep 'java -jar start.jar'| grep -v grep | cut -c1-6

Step by Step:

  • ps -e: show all the pids

  • ps -o pid,args: specify the fields to show:

    $ ps -eo pid,args 12498 -bash 14132 java -jar start.jar 17489 grep java -jar start.jar ...

  • grep 'java -jar start.jar': only leave the specified commands:

    $ ps -eo pid,args | grep 'java -jar start.jar' 14132 java -jar start.jar 17489 grep java -jar start.jar

  • grep -v grep: exclude the grep itself:

    $ ps -eo pid,args | grep 'java -jar start.jar'| grep -v grep 14132 java -jar start.jar

  • cut -c1-6: cut the first 6 characters:

    $ ps -eo pid,args | grep 'java -jar start.jar'| grep -v grep | cut -c1-6 14132

Other ways to get pid: echo $$ or echo $!.

Start Daemon

if kill -0 $pid_mongod > /dev/null 2>&1; then
    echo Database is already running ...
else
    echo Starting Database ...
    bin/mongod &
fi

Step by Step:

  • kill -0 $pid_mongod: -0 for testing if the pid is available to kill. It will return 0 if you can kill , 1 if is invalid or you do not have access.
  • ... > /dev/null 2>&1: redirect the output to /dev/null, a blackhole. 2 stands for STDERR, 1 for STDOUT. This command means redirect STDOUT to /dev/null, and redirect STDERR to STDOUT(which is /dev/null), so both STDOUT and STDERR will be quiet. Remember the & before destination.
  • bin/mongod &: if it is not running, start the daemon. Remember the & for running in the background. Wait before it is up:
while true; do
    sleep 1
    if pgrep mongod; then
        break
    fi
done

Stop Daemon

Similar to the previous section.

if kill -0 $pid_mongod > /dev/null 2>&1; then
    echo Stopping mongod Server ...
    kill -9 $pid_mongod
else
    echo No mongod Server to stop ...
fi

Nohup

Run command in background and redirect output to file.

nohup command > output &

Quote from Wikipedia: nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout. Then use

tail -f output

to print the last few lines of the output.

Run at a specific time

$ at 2:00 PM
cowsay 'hello'
(CTRL + D)

the command will run at 2 PM

List all jobs

$ at -l
1	Tue Apr 12 14:00:00 2016

Remove a job

$ at -r 1

Cron

/etc/crontab schedules /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly.

Scripts in those folders will be executed.

By default cron jobs logs can be found in /var/log/syslog

$ grep CRON /var/log/syslog