process control

# lists jobs in current session
$ jobs

# start job 1 (sleeps for 200 seconds)
$ sleep 200

# suspended job 1 using control-z
[1] + Stopped sleep 200

# start job 2 (sleeps for 400 seconds)
$ sleep 400

# suspended job 2 using control-z
[2] + Stopped sleep 400

# lists jobs in current session shows the jobs are suspended i.e. not running
$ jobs
[2] + Stopped sleep 400
[1] – Stopped sleep 200

# place sleep 400 in background
$ bg %2
[2] sleep 400

# shows job status sleep 400 is running and 1 is stopped/suspended
$ jobs
[1] + Stopped sleep 200
[2] – Running sleep 400

# place sleep 200 in background
$ bg %1
[1] sleep 200

# shows job status both are running
$ jobs
[1] + Running sleep 200
[2] – Running sleep 400

# place sleep 200 job in foreground
$ fg %1
sleep 200

# pressed control-z to suspend sleep 200
[1] + Stopped sleep 200

# show job status sleep 200 is suspended and sleep 400 is running
$ jobs
[1] + Stopped sleep 200
[2] – Running sleep 400

# wait for suspended job wait will return immediately as it is not running
$ wait %1

# show job status sleep 200 is suspended and sleep 400 is running
$ jobs
[1] + Stopped sleep 200
[2] – Running sleep 400

# place job 1 (sleep 200) in background so it will run
$ bg %1
[1] sleep 200

# wait tells to wait for sleep 200 to finish
$ wait %1

[2] + Done sleep 400

Advertisements

Background process

There are multiple ways to place a program in background mode. Background mode is when the program runs and returns you the prompt back and the program is till running but it can be tied to the session or untied to the shell.

The following scenario is when a process is tied to a shell:
Example: $ sleep 60 &
In the above example you are starting “sleep” in background, it will return the prompt back to you while the program runs.

In the case if the program is started but not in background mode, by pressing Ctrl-Z will suspended the program and return the prompt back to you, at that point the program is no longer running i.e. is suspended, so typing “bg” will place the program in background mode and return the prompt back to you.

Example: $ sleep 60
<Press Ctrl-Z>
Stopped
$ bg

By using nohup infront of the command the command is not tied to shell, example:
$ nohup sleep 60 &

ksh – Process

— Get process id of current process
$ echo $$
6113
— shows process id
$ ps
PID TTY TIME CMD
2050 pts/2 00:00:00 ps
6113 pts/2 00:00:00 ksh

Useful when creating a unique temp file example TEMPFILE=/tmp/tmpfile.$$

— Get process id of last background id
$ sleep 10 &
[1] 2078
$ echo $!
2078

— Print parent process id
$ print $PPID
6048

How to change unix prompt?

C-shell (csh)
Example: set prompt=”[%n@%m %c] \! %”
oracle@localhost ~] 39 %
In this above example sets prompt to display username, hostname, current working directory and history number.
%n – username, %m – hostname, %c – pwd, \! – history number

K-shell (ksh)
Example: $ export PS1=’$USER [$PWD] $ ‘
oracle [/home] $
In the above example, sets prompt to display username and current working directory.

whereiz – find all version of a command

Using the following script it will find all version of a command in $PATH (from Unix Power Tools)

$ cat /tmp/whereiz
#!/bin/sh
testx=”test -x”

fixpath=”`echo $PATH | sed \
-e 's/^:/.:/' \
-e 's/::/:.:/g' \
-e 's/:$/:./'`”

# echo $fixpath
IFS=”: ”
for command
do
where=””
for direc in $fixpath
do $testx $direc/$command && where=”$where $direc/$command”
done
case “$where” in
?*) echo $where ;;
esac
done

Example
$ /tmp/whereiz ls grep
/bin/ls /tmp/ls
/bin/grep

Run a logout script in k-shell

By setting trap one can run a logout script when a k-shell is being terminated. The built-in command trap in k-shell allows one to customize shell when a signal is received.

# Set to run $HOME/.logout script when shell is terminated
$ trap ‘. $HOME/.logout’ 0
# display list of signals setup
$ trap
trap — ‘. $HOME/.logout’ EXIT

grep options

Here are some of the options that can be passed to grep:
-l – Print filename if pattern found otherwise it doesn’t print the filename, if found any one file exit status is 0, if not found in any files then 1
-n – Print line # where pattern was found
-i – ignore case when matching string
-v – Display lines where the given pattern doesn’t match
-c – Display total number of occurrences of pattern found, if multiple occurrences on one line it is counted as one
-s – silent displays except error message, if found then exit status is 0 else non-zero