Linux Shell &, &&, | |

Source: Internet
Author: User
Tags switches

& placed after the startup parameter to set this process as a background process

By default, the process is the foreground process, then the shell is occupied, we can not do other operations, for those who do not interact with the process, many times, we want to start in the background, you can start the parameters with a ' & ' to achieve this purpose.

Such as:

Tianfang > Run &
[1] 11319
Tianfang >

when the process switches to the background, we call it job. When switching to the background, the relevant job information is output, with the previous output as [1] 11319 Example: [1] indicates that the job ID is 1,11319 indicates that the process ID is 11319. A process that switches to the background can still be viewed with the PS command.

switching between front and rear tables

The BG <jobid> (background) and fg<jobid> (foreground) commands enable them to switch between the pre-background state.

Daemon Process

If a process is always started in the background and cannot be exited by a shell exit, it is a legitimate practice to create it as a daemon. The daemon is worth the long-running background process of the system, similar to Windows services. Daemon information through Ps–a can not see, need to use the –x parameter, when using this command, often also attach-j parameters to view job control information, where Tpgid column is 1 is the daemon process.

Tianfang > PS XJ
PPID PID pgid SID TTY tpgid STAT UID time COMMAND
953 1190 1190 1190? -1 Ss-0:00/bin/sh/usr/bin/startkde
1 1490 1482 1482? -1 Sl 0:00/usr/bin/vboxclient–seamless
1 1491 1477 1477? -1 Sl 0:00/usr/bin/vboxclient–display

The most critical step in creating a daemon is to call the SETSID function to create a new session and become the session Leader. The result of a successful call to this function is:

    • create a new session, the current process becomes session Leader, the ID of the current process is the session ID
    • Create a new process group, the current process becomes the leader of the process group, and the ID of the current process is the ID of the process group
    • if the current process originally had a control terminal, it would lose the control terminal and become a process without the control terminal.

For more information, please refer to: http://www.cnblogs.com/TianFang/archive/2013/01/23/2872645.html


&&

When the shell executes a command, it returns a return value that is stored in the shell variable $?. When $? = = 0 o'clock, indicating successful execution; = = 1 O'Clock (I think the number is 0 and the return value is 0-255), indicating that the execution failed.

Sometimes the next command depends on whether the previous command succeeds. For example, execute another command after executing a command successfully, or execute another command after a single command fails. The shell provides && | | To implement command execution control, the shell will be based on && or | | The return value of the preceding command to control the execution of subsequent commands.

The syntax format is as follows:

Command1 && command2 [&& Command3 ...]

1 use && Connect between commands to achieve Logic andThe function. 2 only the command on the left of && returns True (command return value $?).  = = 0) The command to the right of,&& will be executed. 3 As long as there is a command return False (command return value $?) = = 1), the subsequent command will not be executed. --Short-circuit function

Command1 | | command2 [| | | Command3 ...]

1 Use between commands | | Connect, implement Logical ORThe function. 2 Only in | | The left command returns False (the command returns a value of $?). = = 1), | | The command on the right will not be executed.  This is the same as the logical or syntactic function in C, which is to implement short-circuit logic or operations. 3 As long as there is a command return True (command return value $?) = = 0), the subsequent command will not be executed. --until it returns to the real place to stop execution.

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

Related recommendations:

The difference between & and && in Java

In Java, and when you connect two numbers, it is the bitwise and of the two bits, while the && represents the logic and operation of the two conditions, and the function of short circuit. However & if 2 conditions are connected, it will be executed.

--"key points: First say the most obvious differences, say the same point."

Linux Shell &, &&, | |

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.