jobs ps aux bg/fg ctrl+z nohup ps fax

every command you start from the shell is considered as shell job

jobs are visible in current shell for current user

separate shell for current user will show nothing with jobs command

if you want to keep job running after closing shell, use nohup
[centos@localhost ~]$ nohup sleep 600 &

The name nohup stands for "no hangup." The hangup (HUPsignal, which is normally sent to a process to inform it that the user has logged off (or "hung up"), is intercepted by nohup, allowing the process to continue running.

When you execute a Unix job in the background ( using &, bg command), and logout from the session, your process will get killed. You can avoid this using several methods — executing the job with nohup, or making it as batch job using at, batch or cron command.



Example: Printing lines to both standard output & standard error

while(true)
do
echo "standard output"
echo "standard error" 1>&2 
sleep 1;
done

Execute the script with redirection

$ nohup sh custom-script.sh > custom-out.log &
[1] 11069
$ nohup: ignoring input and redirecting stderr to stdout

$ tail -f custom-out.log
standard output
standard error
standard output
If you log-out of the shell and login again, you’ll still see the custom-script.sh running in the background.
$ ps aux | grep sathiya 
sathiya  12034  0.0  0.1   4912  1080 pts/2    S    14:10   0:00 sh custom-script.sh


[root@localhost ~]# ps aux | sort -rk 3 | more
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      4793 78.4  0.0 107940   608 pts/1    R    15:45   0:56 dd if=/dev/zero of=/dev/null
centos    3012  0.2  2.5 573496 26072 ?        Sl   14:56   0:07 /usr/bin/konsole --workdir /home
/centos
polkitd    699  0.1  1.2 541912 12676 ?        Ssl  14:56   0:03 /usr/lib/polkit-1/polkitd --no-d
ebug


RSS is the Resident Set Size and is used to show how much memory is allocated to that process and is in RAM. It does not include memory that is swapped out. It does include memory from shared libraries as long as the pages from those libraries are actually in memory. It does include all stack and heap memory.
VSZ is the Virtual Memory Size. It includes all memory that the process can access, including memory that is swapped out and memory that is from shared libraries.
So if process A has a 500K binary and is linked to 2500K of shared libraries, has 200K of stack/heap allocations of which 100K is actually in memory (rest is swapped), and has only actually loaded 1000K of the shared libraries and 400K of its own binary then:
RSS: 400K + 1000K + 100K = 1500K
VSZ: 500K + 2500K + 200K = 3200K
Since part of the memory is shared, many processes may use it, so if you add up all of the RSS values you can easily end up with more space than your system has.

? - if this is a background process, you see ? in ps
PROCESS STATE CODES
       Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process:
       D    uninterruptible sleep (usually IO)
       R    running or runnable (on run queue)
       S    interruptible sleep (waiting for an event to complete)
       T    stopped, either by a job control signal or because it is being traced.
       W    paging (not valid since the 2.6.xx kernel)
       X    dead (should never be seen)
       Z    defunct ("zombie") process, terminated but not reaped by its parent.

       For BSD formats and when the stat keyword is used, additional characters may be displayed:
       <    high-priority (not nice to other users)
       N    low-priority (nice to other users)
       L    has pages locked into memory (for real-time and custom IO)
       s    is a session leader
       l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
=========================
[root@localhost ~]# dd if=/dev/zero of=/dev/null
^Z
[1]+  Stopped                 dd if=/dev/zero of=/dev/null

[root@localhost ~]# jobs
[1]+  Stopped                 dd if=/dev/zero of=/dev/null

[root@localhost ~]# bg
[1]+ dd if=/dev/zero of=/dev/null &

[root@localhost ~]# jobs
[1]+  Running                 dd if=/dev/zero of=/dev/null &
[root@localhost ~]#

The general job control commands in Linux are:

jobs - list the current jobs
fg - resume the job that's next in the queue
fg %[number] - resume job [number]
bg - Push the next job in the queue into the background
bg %[number] - Push the job [number] into the background
kill %[number] - Kill the job numbered [number]
kill -[signal] %[number] - Send the signal [signal] to job number [number]
disown %[number] - disown the process(no more terminal will be owner), so command will be alive even after closing the terminal.
That's pretty much all of them. Note the % infront of the job number in the commands - this is what tells kill you're talking about jobs and not processe



w - shows how many terminals open 
[centos@localhost ~]$ w
 16:22:08 up  1:26,  5 users,  load average: 1.60, 1.49, 1.37
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
centos   :0       :0               Sat16   ?xdm?  60.86s  0.15s gdm-session-worker [pam/gdm-pass
centos   pts/0    :0               Sat16    3days  0.00s  2.12s kdeinit4: kded4 [kdeinit]      
centos   pts/1    :0               Sat16   16.00s 30:59   0.03s /bin/bash
centos   pts/2    :0               16:00   32.00s  0.03s  0.03s /bin/bash
centos   pts/3    :0               16:22    0.00s  0.03s  0.00s w

ps fax
if there is an issue with killing process, you can kill parent process
3012 ?        Sl     0:15 /usr/bin/konsole --workdir /home/centos
 3021 pts/1    Ss     0:00  \_ /bin/bash
 3079 pts/1    S      0:00  |   \_ su -
 3082 pts/1    S      0:00  |       \_ -bash
 3493 pts/1    S      0:00  |           \_ su - kevin


Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all