Linux background Running Program (SECURECRT after it is broken or shut down to continue running the program)

Source: Internet
Author: User
Tags session id terminates

SECURECRT to continue running the program after it has broken down. That is, the program will be executed in the background until the end.

The SECURECRT is connected to the client side via SSH, and the task is killed if the window is closed or disconnected.


For the program to be able to perform backstage to the end:

1. Can be used &:

Like Python test.py to Python test.py &

to invoke execution.

2. In addition, there is another way: through a series of commands, such as CTRL + Z, BG, the operation has been run in the foreground in the background execution:

If a job is already executing in the foreground, you can put the job in the background and hang it by Ctrl + Z. Then, through the Jobs command, view the jobs performed in the background and find the corresponding job ID, and execute BG%n (n for the job ID that was found by jobs) to continue execution.
There is also the case that the result is output to the terminal, and the same can be solved by the redirection method
Related commands:
Jobs------------View processes executed in the background
FG%n----------The background execution process n to the foreground, n means Jobnumber (the process number viewed by jobs, not PID)
CTRL + Z----------will be executed in the foreground process, put in the background and hang
BG%n---------will be suspended in the background process, continue to execute
CTRL + C----------The foreground process terminates
Kill%n---------kills the process running in the background, n means Jobnumber (the process number viewed by jobs, not PID)


But the above two methods can only solve the problem when disconnect.

If you close the client, the program cannot execute. When the user logs off or the network is interrupted, the terminal receives a SIGHUP signal, which closes all its child processes.

This will be done using nohup


3.nohup commands: Nohup + command

such as Nohup python test.py

Nohup is not suspended, the Nohup command ignores the sighup signal, so the terminal exits without affecting the background job. background Run the terminal output is stored in the Nohup.out file in the current directory. After the client shuts down, the server can be viewed directly.


4. In addition to the screen command mode can also be implemented, not introduced.



After running the program with

Ps-ef | grep ' Carry ID '

or Ps-aux to see how much later it's going to be running, grep's own user name, you can see the corresponding program



The principle is as follows:

In Linux/unix, there are several concepts:
Process group: A collection of one or more processes, each with a unique process group ID, which is the ID of the process leader process.
Session Duration: A collection of one or more process groups, with a unique session-period-first process (session leader). The ID of the session ID that is the lead process.
The session period can have a separate control terminal (controlling terminal). The session first process connected to the control terminal is called the control process (controlling processes). The process that is currently interacting with the terminal is called the foreground process group. The remaining process groups are called background process groups.
According to POSIX.1 definition:
Hang-up signal (SIGHUP) The default action is to terminate the program.
When the terminal interface detects a disconnected network connection, the hang-up signal is sent to the control process (session-first process).
If the session first process terminates, the signal is sent to the foreground process group for that session period.
A process exit causes an orphan process group to occur if any of the orphaned process group processes are in the stop state, sending Sighup and sigcont signals to all processes in the process group.
Therefore, when the network is disconnected or the terminal window is closed, the control process receives a SIGHUP signal exit, which causes the other process to exit during the session.



test.py test Script:

[email protected] test]$ cat test.py
Import time
Dict = {"A": "Apple", "B": "Banana", "G": "Grape", "O": "Orange"}
For K in Dict:
Print "dict[%s]="%k,dict[k]

Key= "C"
If "C" not in Dict:
Print "It isn't in%s"%key

Print "-------------"
Print Dict.items ()
Print Dict.keys ()
Print dict.values ()

Print "-------------"
iter = Dict.iteritems ()
For it in ITER:
Print "Iteritems is:", it
Print type (IT)

Print "-------------"
Key_iter = Dict.iterkeys ()
For KI in Key_iter:
Print "Key_iter is", Ki
Print Type (KI)


Print "-------------"
Val_iter = Dict.itervalues ()
For VI in Val_iter:
Print "Val_iter is", VI
Print type (vi)
Print "-------------"

Print "Start:%s"% time.ctime ()
Time.sleep (20)
Print "End:%s"% time.ctime ()

F=open (' Result ', ' W ')
F.write (' This was my test for breakup, and to find whether we can finish job ')

Time.sleep (20)
F.write (' \nafter We break the dialog ')
F.close

Nohup python test.py after execution. After you close the client and log back in, you can view the Nohup.out to see the run-time screen output. View result at the same time.



Linux Daemon (SECURECRT or shutdown after running the program)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.