Subscribe:

Ads 468x60px

Pages

Showing posts with label Process. Show all posts
Showing posts with label Process. Show all posts

Friday, July 15, 2011

Running program when terminal closed?

Question: I started the process and move a process to background by using bg command or Ctrl+z or ampersand symbol (&) from the terminal. But when I am closed the terminal that also terminates the background process. How to I ensure that the background processes is not terminated even after i close the terminal?

Solution: 
Most of the time you running process in background in the terminal via bg or Ctrl+z or ampersand symbol (&). If you start a command and you closed the terminal, the process / command will get killed. This is because Linux Operating System, by default will end the process if you exit your terminal. Sometime the  job or command takes a long time ,  it is not secure to let your terminal open while you are not there, or maybe not possible to let it open if you are over a running process in background, so you may want to use nohup command to let the process running even when you exit your terminal. If you are not sure when the job will finish, then it is better to leave job running in background. However, if you logout the system, the job will be stopped. What do you do? For this use any one of the command to run process in background even if you exit from terminal or logout from your session.
1.     use the “screen” command
2.     use the  “nohup” command
3.     use the “disown” command

1. The “screen” utility: Just open a shell terminal and type screen  -ls . You get the name(s) of the screen session-terminal(s)  where a process was set to be run in back-ground (bg) . Now use “screen -R session-terminal name ” and BINGO , you can manipulate the process if it is still running (not finished his job).

2. The “nohup” utility is an  ” out of the box” utility , just run any command but in front set the keyword “nohup“  example :  nohup  cat /dev/zero > /dev/null  &
Be sure to add the & sign so the command is executed in background. Now even if  the terminal is closed , the process / command will continue to run . Just open a new terminal and run :
ps -aux |grep nameof-command (ps aux |grep cat ) and BINGO you get the PID of the process.

3. The “disown” command has the same functionality but is implemented differently .
Just enable a process to run in the background with the &  . The terminal will assign a job-ID to this process , let’s say 5065 . Now use disown 5065 , and the process will be inherited by the init process , so even if the terminal is closed the process will keep running.

How does disown and nohup commands work? 
What they do is that they connect all your process to the parent process of the computer which happens to be init (the parent process of all process running on the computer). Viewing all the process on the computer, use pstree command , the init is first (root of the inverted tree) process which radiates all the other process in the computer. Now if you run the top command you will see all the processes that were manipulated with disown and nohup with a PPID of 1 (init) .

Please feel free to comment to make it more useful to everyone.

Read more...

Monday, June 20, 2011

Process Management


Process:
Any application that runs on a Linux system is assigned a process ID or PID. This is a numerical representation of the instance of the application on the system. In most situations this information is only relevant to the system administrator who may have to debug or terminate processes by referencing the PID. Process Management is the series of tasks a System Administrator completes to monitor, manage, and maintain instances of running applications.

Type of Process: 
1. Interactive Process  
2. System Process (daemon)

Interactive Process:
    The processes that are invoked by a user and can interact with the user is called Interactive processes. Interactive processes can be classified into foreground and background processes. Foreground process is the process that you are currently interacting with, and is using the terminal as its stdin (standard input) and stdout (standard output). Background process is not interacting with the user and can be in one of two states – paused or running.

Example of  Interactive process: 
The following example will show how foreground and background processes are running.
1. Logon as root.
# su -

2. Run [cd \]
# cd \

3. Run [vi]
# vi data.txt

4. Press [ctrl + z]. This will pause vi
Ctrl + z

5. Type [jobs]
# job
Notice:  vi is running in the background

7. Type [fg %1]. This will bring the first background process to the foreground.
# fg %1

8. Close vi 
:q

System Process:
The process that runs on Linux is a system process or Daemon (day-mon). Daemon is the term used to refer to process’ that are running on the computer and provide services but do not interact with the console. Most server software is implemented as a daemon. Apache, Samba, and inn are all examples of daemons.

Any process can become a daemon as long as it is run in the background, and does not interact with the user. A simple example using the [gedit] command. This will list all text editor window separately on the computer. This command can be set to run in the background by typing [gedit&], and although technically you have control over the shell prompt, you will be able to do little work as the screen displays the output of the process that you have running in the background. 

Notice: The standard pause (ctrl+z) and kill (ctrl+c) commands.

1. View linux process
Show process launched for the current linux user
$ ps
Show process launched for a linux user
$ ps -u username
View all the running linux process
$ ps aux
To see a process tree
$ pstree
To see the processes in real time

2. Stop temporarily processes
$ bigjob
^Z (CRTL+Z)

To see the stopped linux processes 
$ jobs
   [1]+ Stopped bigjob

Kill an stopped process
$ kill %1
   [1]+ Killed bigjob
Resume the linux process
$ fg 1

3. Foreground and background linux processes
To start a process in  background mode
$ bigjob &

See process
$jobs
  [1]+  Running bigjob

Put it in foreground mode
$fg 1

ps command:
The ps command is the used to manage running processes and can be used for many things including viewing the status of your computer and knowing how well the computer is performing.

Some common ps commands: 
View display currently running process on this terminal.
ps

All current processes on this terminal, by all users.
# ps –a

All processes not assigned to a terminal (daemons).
ps –x 

To get information about all running process
# ps –ag

To kill all process except your shell
# kill 0

Start a process in background
# linux-command &

To get all the details regarding the running process.
# ps aux

To check a particular process.
# ps ax | grep process-name

To see currently running processes and there memory usage.
# top

To see currently running processes in a tree structure.
# pstree 

4 killing linux  processes
·          kill -1 pid.    Restart  the process 
·          kill -9  pid .  kill the process
·          kill -15  pid  End  the process 

System calls used for Process management:
  • Fork () :- Used to create a new process
  • Exec() :- Execute a new program
  • Wait():- wait until the process finishes execution
  • Exit():- Exit from the process
  • Getpid():- get the unique process id of the process
  • Getppid():- get the parent process unique id
  • Nice():- to bias the existing property of process  
Signals:
 Various Signals are:
Signal Name
Number
Description
SIGHUP
1
Hangup (POSIX)
SIGINT
2
Terminal interrupt (ANSI)
SIGQUIT
3
Terminal quit (POSIX)
SIGILL
4
Illegal instruction (ANSI)
SIGTRAP
5
Trace trap (POSIX)
SIGIOT
6
IOT Trap (4.2 BSD)
SIGBUS
7
BUS error (4.2 BSD)
SIGFPE
8
Floating point exception (ANSI)
SIGKILL
9
Kill(can't be caught or ignored) (POSIX)
SIGUSR1
10
User defined signal 1 (POSIX)
SIGSEGV
11
Invalid memory segment access (ANSI)
SIGUSR2
12
User defined signal 2 (POSIX)
SIGPIPE
13
Write on a pipe with no reader, Broken pipe (POSIX)
SIGALRM
14
Alarm clock (POSIX)
SIGTERM
15
Termination (ANSI)
SIGSTKFLT
16
Stack fault
SIGCHLD
17
Child process has stopped or exited, changed (POSIX)
SIGCONT
18
Continue executing, if stopped (POSIX)
SIGSTOP
19
Stop executing(can't be caught or ignored) (POSIX)
SIGTSTP
20
Terminal stop signal (POSIX)
SIGTTIN
21
Background process trying to read, from TTY (POSIX)
SIGTTOU
22
Background process trying to write, to TTY (POSIX)
SIGURG
23
Urgent condition on socket (4.2 BSD)
SIGXCPU
24
CPU limit exceeded (4.2 BSD)
SIGXFSZ
25
File size limit exceeded (4.2 BSD)
SIGVTALRM
26
Virtual alarm clock (4.2 BSD)
SIGPROF
27
Profiling alarm clock (4.2 BSD)
SIGWINCH
28
Window size change (4.3 BSD, Sun)
SIGIO
29
I/O now possible (4.2 BSD)
SIGPWR
30
Power failure restart (System V)

Read more...