Let Linux programs run in the background

Source: Internet
Author: User
Tags usleep

Recently to use PHP process to send the reward, because to be accurate to the second execution, the Linux system timing is not used, so wrote a dead loop, usleep is necessary, otherwise the system CPU occupies a very high system CPU.

A problem was encountered when executing, and the process was terminated after shutting down the terminal. The workaround is documented here.

Define(' In_web ', ' true ');Set_time_limit(0);include(dirname(__file__)." /.. /com.php ");$pidFile= Path_dat. ' Ticket.pid ';if(file_exists($pidFile)){    Exit(' Process is exsits! ');}file_put_contents($pidFile,posix_getpid ()); while(true){    if( Time()% 30 = = 0){        //doing    }    Usleep(1000);}

Sometimes, we need to start a program in the terminal and make it run-but if you close the terminal, then the program is closed. So is there any way to keep the programs that have been started from this terminal running after shutting down the terminal?

Pre-Knowledge: the difference between xterm,console,tty,pts,pty
  • The shell is something that communicates directly with the kernel.
  • Xterm is a software concept that can be connected to the console via this program to control the host, which can be understood as a CLI-like terminal emulator, while Gnome-terminal,konsole is a GUI-style terminal emulator
  • Console is the console of a host and is a physical concept.
  • TTY, Pty, PTS are terminals and are hardware or device concepts.
  • TTY is a generic term for all end devices
  • Pty is one of them, pseudo terminal, or virtual terminal
    • "&" command line End method:

Under Unix/linux If you want the program to run independently of the terminal, it is generally used & at the end of the command to let the program run automatically. (Can not append space after command)

Open gnome-terminaland execute the following command:

There are a few points to note:
      1. The program that has been started is still attach to the current PTS, and only the current terminal emulator is closed (exiting with the Exit command), and the process is automatically inherited by the TTY.
[email protected]:~$ ps-e | grep totem//program has been started in Totem &, currently attached to Pts0 8819 pts/0 00:00:00 totem[email Protect ed]:~$ PS-E |        grep Totem//PTS0 's analog terminal is closed by the Exit command, Totem automatically attached to TTY8819? 00:00:00 Totem[email protected]:~$
      1. Process with debug output, you need to press the ENTER key to interrupt the current debug output. However, if the program continues to printf, you will not be able to enter any commands.
[email protected]:~$ VLC &[1] 8850[email protected]:~$ VLC media player 1.0.6 Goldeneye[0x8b998b0] Main Libvlc:running VLC with the default interface. Use ' CVLC ' to use VLC without interface.                      &NBSP ;                          ,         &NB Sp    //enter pressed[email protected]:~$                   &NBSP ;           //show a clean terminal now** (: 8850): CRITICAL * *: giop_thread_request_push:assertion ' Tdata! = NULL ' failed                            nbsp                                 //Still In output data ......                      &NBSP                          ,         &NB Sp    //shutdown program [1]+ done vlc[email protected]:~$
    1. You cannot log the debug output of the program.
    2. This terminal can be closed only if the virtual terminal is $ or #, otherwise it may cause the process that has started to be closed (press enter--If the program continues to output information without appearing $ or #)
    • Use the Nohup command :

Nohup Description: Run command, ignoring hangup signals. (Ignore any interrupt/suspend signal to keep the command executing)

But when you try to use the command:

1 nohup command

You will encounter a moderate problem ...

[Email protected]:~$ nohup vlcnohup:ignoring input and appending output to ' nohup.out '

Yes, although it automatically logs debug information to the Nohup.out file, you can't do anything with this terminal.

So you need to mix with the first method, namely

nohup command {option} &

When mixed, it automatically logs the output of the command you executed to a file named nohup.out with the permission -RW ——-.

But you still need

[Email protected]:~$ nohup VLC &[1] 9045[email protected]:~$ nohup:ignoring input and appending output to ' nohup.out '                                                         Click here to enter or CTRL + C to [email protected]:~$ 

As with the use of the "&" nature, the current launcher terminal if not closed, the program has been started attached to the PST, if the terminal is closed, it is automatically attached to the TTY.

If the nohup.out file for the current directory is not writable, the output is redirected to the $HOME/nohup.out. By default,nohup output to the nohup.out file by default, and you can also use redirection to specify the output file:

nohup command {option} > Myout.file 2>&1 &

This terminal can be closed only if the virtual terminal is $ or #, otherwise it may cause the process that has started to be closed (press enter--If the program continues to output information without appearing $ or #)

    • Other Related commands:

jobs: See how many commands are currently running in the background
FG: The command in the background is moved to the foreground to continue running. If there are multiple commands in the background, you can use FG%jobnumber to bring up the selected command,%jobnumber is the ordinal (not PID) of the command being executed in the background through the jobs command.
BG: A command that pauses in the background changes to continue execution. If there are multiple commands in the background, you can use BG%jobnumber to bring up the selected command,%jobnumber is the ordinal (not PID) of the command being executed in the background through the jobs command.

    • Kill process

Killing a program that has already started is the same as the normal way:

  • Pkill-9 Name
  • Killall Name
  • Kill PID
  • ...
    • Command application:

Linux server hangs the machine to download; start related services; Linux server startup process (especially SSH login)

        I've been using it
    .
1 nohup aria2c -i downloadlist -m 0 -j 1 &

Enjoy downloading a lot of anime from Jsharer.

    • Resources

Http://topic.csdn.net/u/20100201/17/a34370cc-8a61-4315-a4d0-84242362064d.html
Http://www.linuxsir.org/bbs/thread362001.html
Http://www.williamlong.info/archives/482.html
Http://dev.firnow.com/course/6_system/linux/Linuxjs/2008716/133186.html

Let Linux programs run in the background

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.