Automatically run shell in the background

Source: Internet
Author: User
Tags exit in
Use the nohup command to make Linux programs always run in the background (from: http://www.einit.com/user1/11/archives/2006/3603.html)

In Unix/Linux, you generally want to run a program in the background. Many of them use & at the end of the program to run the program automatically. For example, to run MySql in the background:

/Usr/local/MySQL/bin/mysqld_safe -- user = MySQL &

However, many of our programs do not support daemon like mysqld. Maybe our programs are just common programs &
If the terminal is closed, the program is also closed. To be able to run in the background, we need to use the nohup command. For example, we have a start. Sh to run in the background, and we want
If the backend can always run, use nohup:

Nohup/root/start. Sh &

After you press enter in shell, the following message is displayed:

[~] $ Appending output to nohup. Out

The standard output of the original program is automatically redirected to the nohup. Out file in the current directory, which plays the role of log.

But sometimes there is a problem at this step. When the terminal is closed, the process will be automatically closed. Check nohup. Out to see that the service will be automatically closed when the terminal is closed.

After consulting with the red-flag Linux engineer, he could not solve the problem. After executing the command on my terminal, the process he started was still running after the terminal was closed.

During the second demonstration to me, I found that I had a different detail from the one I used when operating the terminal: he needed to press any key on the terminal to return to the terminal after the shell prompts that nohup is successful.
Shell enters the command window and then enters exit in shell to exit the terminal. I click Close program to close the terminal every time the nohup is executed successfully .. So at this time
The session corresponding to this command is disconnected, so that the process corresponding to nohup is notified and shutdown is required together.

I did not notice the details, so I recorded them here.

Appendix: nohup command reference

Nohup command

Purpose: run the command without hanging up.

Syntax: nohup Command [arg...] [&]

Description: The nohup command runs the command specified by the command parameter and any relevant Arg parameter, ignoring all sighup signals. Use
The nohup command runs the program in the background. To run the nohup command in the background, add the & (symbol indicating "and") to the end of the command.

Whether or not the output of the nohup command is redirected to the terminal, the output will be appended to the nohup. Out file in the current directory. If
The nohup. Out file cannot be written, and the output is redirected to the $ home/nohup. Out file. If no file can be created or opened for append, the command
The command specified by the parameter cannot be called. If a standard error is a terminal, write the specified command to all outputs of the standard error as the standard output and redirect it to the same file descriptor.

Exit status: This command returns the following export values:

126 you can find but cannot call the command specified by the command parameter.

127 The nohup command has an error or cannot find the command specified by the command parameter.

Otherwise, the exit status of the nohup command is the exit status of the command specified by the command parameter.

Nohup command and output file

Nohup command: If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account/Close the terminal. Nohup means not to suspend (N ohang up ).

The command is generally in the form of nohup command &

Use the nohup command to submit a job

If you use the nohup command to submit a job, all the output of the job is redirected to a file named nohup. Out by default, unless the output file is specified separately:

Nohup command> myout. File 2> & 1 &

In the preceding example, the output is redirected to the myout. File file.

Use jobs to view tasks.

Disable using FG % N.

There are also two commonly used FTP tools ncftpget and ncftpput, which can be used to upload and download files in the background through FTP, so that you can use these commands to upload and download files in the background.

Related Article

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.