Run linux Command in the background

Source: Internet
Author: User
Tags autoload

1. Add "&" after the command to make the command work in the background. The biggest advantage of this operation is that it is not afraid of being interrupted by the ctrl + c command.

2. You may have to ask again how the program executed in the background restores it to the foreground for running? It's easy. Just execute the command fg.

3. Some people may ask again. Can I put the command I have run on the front-end and run it in the background? Of course, you can do it by executing ctrl + z. Is it awesome!

4. Some may ask again. If I have multiple processes running in the background, how can I restore them to the foreground for execution? At this time, the jobs command will be used. Through the jobs command, all processes executed in the background can be listed. The number in the brackets ([]) is the code of jobs, you can use fg % number to restore the specified background process.

Use nohup to run the program on the remote host background

Because I purchased a foreign host with the Telnet permission, I also used Telnet to log on to Linux, but I found that the window was automatically disconnected from the host once it was closed, unlike Windows terminals, you can find a method to run a program from the background.

In Unix/Linux, for example, if you want a program to run in the background, many of them use & to make the program run automatically at the end of the program. For example, to run mysql in the background:

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

However, joining many of our programs is not a daemon like mysqld. Maybe our program is just a common program. Generally, this program is used and ended, but if the terminal is closed, the program will also be closed. However, in order to be able to run in the background, we can use the nohup command. For example, if we have a test. php that needs to run in the background and want to run it regularly in the background, we can use the nohup command:

Nohup/root/test. php &

Tip:

[~] $ Appending output to nohup. out

Well, it proves that the program runs successfully. At the same time, it puts the output information of the program running in the nohup. out file of the current directory.

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. After logging out, run the nohup command to run the program in the background. To run [...]

1. Two commands running in the background & and ctr + z

At the beginning, the + "&" method is as follows: [command] space [&]

For example:

[Root @ pcmxexweb etc] # find/-name xml &

In this way, the lookup program runs in the background. It does not affect other tasks.

But sometimes I forget to add the '&' symbol after the command, and I am not willing to stop the command and rewrite it again. You can press [ctrl + z] to switch the current program to the background.

[Root @ pcmxexweb etc] # find/-name xml

/Etc/xml

/Usr/lib64/python2.4/xml

/Usr/share/vim/vim70/autoload/xml

/Usr/share/maid/data/xml

/Usr/share/java-utils/xml

/Usr/share/xml

/Usr/share/doc/libvorbis-devel-1.1.2/xml

/Usr/local/apache-tomcat-6.0.14/webapps/examples/jsp/xml

/Webapp/web/jsp/xml

[Root @ pcmxexweb etc] # find/-name xml

/Etc/xml

/Usr/lib64/python2.4/xml

/Usr/share/vim/vim70/autoload/xml

/Usr/share/maid/data/xml

/Usr/share/java-utils/xml

/Usr/share/xml

[1] + Stopped find/-name xml

However, note that the program in the background is in the Stopped state (check the last line above ).

Use jobs to view programs running in the background.

[Root @ pcmxexweb etc] # jobs

[1]-Stopped find/-name xml

[2] + Stopped vi/etc/hosts

You can use the command bg 2. The effect is the same as that of using find/-name xml & at the beginning, so that this command runs in the background.

2. Transfer the background program back to the foreground

According to jobs, the corresponding program is redeployed back to the front-end by using fg 2.

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.