Linux execution shell scripts and differences & commands background Run __linux

Source: Internet
Author: User
Tags rsync
Original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, author information and this statement. Otherwise, legal liability will be held. http://4554480.blog.51cto.com/4544480/837006

Suppose the shell script file is hello.sh
Put it in the/root directory. Here are several ways to execute shell scripts at a terminal:

[Root@localhost home]# cd/root/

[root@localhost ~] #vim hello.sh

#! /bin/bash

Cd/tmp

echo "Hello guys!"

echo "Welcome to my blog:linuxboy.org!"

1. Switch to the directory where the shell script is located and execute:

[Root@localhost ~]#./hello.sh

-bash:./hello.sh: Not Enough permissions

2. Execute in an absolute way:

[Root@localhost ~]#/root/desktop/hello.sh

-bash:/root/desktop/hello.sh: Not enough permissions

3. Execute directly with bash or SH:

[Root@localhost ~]# Bash hello.sh

Hello guys!

Welcome to my blog:linuxboy.org!

[Root@localhost ~]# pwd

/root

[Root@localhost ~]# SH hello.sh

Hello guys!

Welcome to my blog:linuxboy.org!

[Root@localhost ~]# pwd

/root

Note: In the above three ways to execute the shell script, the current shell will open a child shell environment, to execute the shell script, the first two must have execute permission to execute

You can also let the shell script execute in the current shell:

4. Executing in the current shell

[Root@localhost ~]#. hello.sh

Hello guys!

Welcome to my blog:linuxboy.org!

[Root@localhost tmp]# pwd

/tmp

[Root@localhost ~]# Source hello.sh

Hello guys!

Welcome to my blog:linuxboy.org!

[Root@localhost tmp]# pwd

/tmp

For the 4th type, no child processes are created, but are executed directly in the parent process

The difference is because the subprocess cannot change the execution environment of the parent process, so the CD (built-in command, only built-in commands can change the execution environment of the shell) did not succeed, but the 4th has no subprocess, so the CD is successful


--------------------------------------------------------------------------------------------------------------- --


Linux Command background run

There are two ways of doing this:

1. Command &: Background operation, you turn off the terminal will stop running
2. Nohup Command &: Background operation, you can turn off the terminal will continue to run


First, Introduction
Linux/unix difference from the Microsoft platform the biggest advantage is the real multi-user, multitasking. Therefore in the task management also has the characteristic management thought.
We know that on Windows, we either have a program running in the background as a service, or stop the service. Instead of letting the program switch between the foreground background. Linux provides FG and BG commands that allow you to easily schedule tasks that are running. Suppose you find that a program running in the foreground takes a long time, but you need to do something else, you can use Ctrl-z to suspend the program, and then you can see the system prompts:
[1]+ stopped/root/bin/rsync.sh
Then we can schedule the program to run in the background: (the number for the BG is the job number)
#bg 1
[1]+/root/bin/rsync.sh &
To view a running task with the jobs command:
#jobs
[1]+ running/root/bin/rsync.sh &
If you want to bring it back to the foreground, you can use
#fg 1
/root/bin/rsync.sh
In this way, you can only wait for the task to complete on the console.

& Throw the instructions in the background to execute
[Ctrl]+z the foreground task to suspend in the background
Jobs View the working status of the background
FG%jobnumber The backstage task to the front desk to handle
BG%jobnumber The task in the background to handle
Kill Admin background tasks

Second, &

In Linux, when a job is run in the foreground, the terminal is occupied by the job, and when the job is run in the background, it does not occupy the terminal. You can use the & command to put the job in the background. In fact, this is to put the command in a job queue:

$./test.sh &
[1] 17208

$ jobs-l
[1]+ 17208 Running./test.sh &
Be careful when running jobs in the background: commands that require user interaction are not executed in the background, because your machine will be silly there. However, running the job in the background will output the results to the screen, interfering with your work. If a job that runs in the background produces a large amount of output, it is best to redirect its output to a file using the following method:
Command >out.file 2>&1 &
In the above example, 2>&1 indicates that all standard output and error output will be redirected to a file called Out.file. When you successfully submit the process, a process number is displayed that can be used to monitor the process or kill it.
For example: Look for a file named "httpd.conf" and redirect all standard output and error output to a Find.dt file:
# find/etc/httpd/-name "httpd.conf"-print >find.dt 2>&1 &
[2] 7832
After the command was successfully submitted, the system gives its process number 7832. For a command that has already been executed in the foreground, you can also put it back in the background, start by pressing Ctrl+z to suspend the process that is already running, and then use the BG command to put the stopped job in the background, for example, to suspend it using ctrl+z for the tesh.sh that is being performed at the foreground:
$./test.sh
[1]+ Stopped./test.sh

$ bg%1
[1]+./test.sh &

$ jobs-l
[1]+ 22794 Running./test.sh &

However, as in the process above to the background, the parent process is still the process of the current terminal shell, and once the parent process exits, the hangup signal is sent to all child processes, and the child process exits after the Hangup is received. If we want to continue running the process while exiting the shell, we need to use nohup to ignore the hangup signal, or SETSID to set the parent process to the INIT process (process number 1)

$ echo $$
21734

$ nohup./test.sh &
[1] 29016

$ PS-EF | grep test
515 29710 21734 0 11:47 pts/12 00:00:00/bin/sh./test.sh
515 29713 21734 0 11:47 pts/12 00:00:00 grep test
$ setsid./test.sh &
[1] 409

$ PS-EF | grep test
515 410 1 0 11:49? 00:00:00/bin/sh./test.sh
515 413 21734 0 11:49 pts/12 00:00:00 grep test
The above experiment demonstrates the use of Nohup/setsid plus & to make a process run in the background without being affected by the current shell exit. What do you do with a process that is already running in the background? You can use the Disown command:

$./test.sh &
[1] 2539

$ jobs-l
[1]+ 2539 Running./test.sh &

$ disown-h%1

$ PS-EF | grep test
515 410 1 0 11:49? 00:00:00/bin/sh./test.sh
515 2542 21734 0 11:52 pts/12 00:00:00 grep test
There is also a way, even if the process in a subshell execution, in fact, this and setsid similar. method is simple, enclose the command in parentheses ():

$ (./test.sh &)

$ PS-EF | grep test
515 410 1 0 11:49? 00:00:00/bin/sh./test.sh
515 12483 21734 0 11:59 pts/12 00:00:00 grep test
Note: The test environment for this article is red Hat Enterprise Linux as Release 4 (Nahant Update 5), the shell is/bin/bash, and different OS and Shell may command somewhat differently. For example, Aix ksh, there is no disown, but can use nohup-p pid to get disown the same effect.

A more powerful way is to use screen to first create a disconnected mode of the virtual terminal, and then reconnect the virtual terminal with the-r option, any commands executed in it can achieve nohup effect, which is more convenient when multiple commands need to be executed continuously in the background:

$ SCREEN-DMS Screen_test

$ screen-list
There is a screens on:
27963.screen_test (Detached)
1 Socket In/tmp/uscreens/s-jiangfeng.

$ screen-r screen_test Source: http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html

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.