180
sh sys-snap.sh &

What is sh? What is sys-snap.sh? Why I should put & at the end of the line? Can anyone explain the syntax?

Without the & the script won't go back to the prompt till I press Ctrl+C. With & I can press enter and it works.

user4951
  • 10,519
  • 3
    is the double ampersand && different from a single ampersand &? – Charlie Parker Jan 16 '17 at 21:35
  • 1
    @CharlieParker Have you ever found an answer to that? I'm trying to figure out the difference myself. (Edit: Nevermind, found it further down) – Drazen Bjelovuk Feb 09 '19 at 06:32
  • Yes, they are completely different. A && B means "run command B if command A succeeded", and A || B means "run command B if command A failed".

    Examples: 'cd tmp && ls' will run ls only if folder tmp exists. Or in a script: 'cd foobar || exit 10' will stop the execution (exit) if folder foobar doesnt' exist.

    – Michał Leon Jun 14 '21 at 22:10

4 Answers4

139

sh is the default Bourne-compatible shell (usually bash or dash)

sys-snap.sh is a shell script, which contains commands that sh executes. As you do not post its content, I can only guess from its name, what it does. I can find a script related to CPanel with the same file name, that make a log file with all current processes, current memory usage, database status etc. If the script starts with a shebang line (#!/bin/sh or similar), you can make it executable with chmod +x sys-snap.sh and start it directly by using ./sys-snap.sh if it is in the current directory.

With & the process starts in the background, so you can continue to use the shell and do not have to wait until the script is finished. If you forget it, you can stop the current running process with Ctrl-Z and continue it in the background with bg (or in the foreground with fg). For more information, see job control

jofel
  • 26,758
  • 5
    sh in usually only bash or dash in Linux based systems (or recent OS/X). On BSDs, it's usually another variant of Ash (dash is based on NetBSD sh, itself based on the Almquist Shell) or a pdksh derivative. On commercial Unices, it's generally ksh88. – Stéphane Chazelas Aug 10 '13 at 15:00
  • 1
    @jofel: What if the below script is running and by mistaken I have executed Ctrl-C. Will my script will execute or it will get terminated? Ex: sh sys-snap.sh & – Hussain7 Aug 02 '16 at 10:38
  • 2
    @Hussain7 Ctrl-C is sent always to the current process in the foreground. If the shell-script is started with & and therefore in the background, you get only a new shell prompt. – jofel Aug 04 '16 at 12:00
  • 2
    is the double ampersand && different from a single ampersand &? – Charlie Parker Jan 16 '17 at 21:35
  • 9
    @CharlieParker Yes, "&&" is the "and" function. The command after && is called only when the command before succeeds. Example "true && echo works" output "works, but "false && echo no output" does not call the echo function. – jofel Jan 17 '17 at 14:47
  • @jofel so if you did echo works && cmd it would always call cmd because echo always returns true? I guess I am trying to think what happens if the boolean is on the other side as in cmd && BOOL. I am assuming that you said true or false as a simplification, the boolean is actually the output of a command? Since I've seen cmd1 && cmd2. – Charlie Parker Jan 17 '17 at 17:37
  • 1
    @CharlieParker Yes, for &&, the shell uses the return value of a process as boolean (zero=true, non-zero=false). true is a simple program which returns always zero, false returns always 1 (i.e. non-zero). – jofel Jan 18 '17 at 12:10
  • 1
    @jofel, Does & have a different meaning in MAC Applescript? – Pacerier Dec 15 '17 at 22:08
  • 1
    @Pacarier In Applescript, the & is used to concatenate strings and need to be escaped to get different behavior (but this is offtopic here) – jofel Dec 16 '17 at 05:59
128

This is known as job control under unix. The & informs the shell to put the command in the background. This means it continues to run the sys-snap.sh command but returns you to your shell to allows you to continue doing parallel commands.

You can see the list of jobs presently running with the jobs command. You can return to the command (bring to the 'foreground) with use of the fg command. Which pulls the command back to the state where you see no prompt and you have to issue Ctrl-C to kill the process. You can however suspend (pause) that process, issuing Ctrl-Z. This will pause sys-snap.sh and return you to your prompt. You can then background it (as though you had issued it with the &) with the bg command, and it will resume running from it's paused state the Ctrl-Z had put it in.

Note that you can have more than one job at a time (as shown by jobs):

[1]-  Running          sys-snap.sh &
[2]+  Running          another-script.sh &

You can background and foreground them using their job number, %1 will be sys-snap.sh and %2 will be another-script.sh. If you use fg or bg without arguments it will action the command on the job marked by + in the jobs output above.

fg %1

will put sys-snap.sh back into the foreground, whilst leaving another-script.sh in the background.

You can issue the Ctrl-C sequence to running jobs without having to foreground them with the kill command, kill %1 will send the equivalent of Ctrl-C to sys-snap.sh.

If you are using bash shell, the man bash command has a detailed section under the section headed 'JOB CONTROL' going into more detail.

As to the name of sys-snap.sh, under unix file names are arbitrary (with a couple of exceptions like dynamic loader files). There is no requirement for them to have specific file extentions to make them run as a shell script, invoke other commands such as perl or php etc. It is usually used to provide clarity, that in this instance, it is .sh a Shell Script using the Bourne Shell /bin/sh.

The functional part of sys-snap.sh (when you look at it's contents with something like the less command) is the Shebang. On the first line you will probably find one of:

#! /bin/sh
#! /bin/bash
#! /usr/local/bin/bash

or similar. In basic terms, a the command after the #! (such as /bin/sh) is ran and the contents of the rest of the script file is fed to it a line at a time. Note that the file must also be set executable with chmod so that you can run it as a command. If the permissions were not set, then the shebang has no effect, because you would either get the error:

bash: sys-snap.sh: command not found

or if you ran it by explicit path ./sys-snap.sh (. meaning the current working directory) you would get:

bash: ./sys-snap.sh: Permission denied

The other alternative is to leave it without execute permissions and explicitly ask /bin/sh to run it:

/bin/sh sys-snap.sh &
Drav Sloan
  • 14,345
  • 4
  • 45
  • 43
  • 3
    this is a great answer. one possible suggestion is to also add in what nohup does, as it is often used in conjunction with & to start a process and be able to kill the shell that launched it without killing the process. – Tommy Oct 14 '15 at 14:49
  • @Drav, Is it true then that jobs will always return empty if its the first command? – Pacerier Dec 15 '17 at 22:16
  • 2
    Note that using & at the end of a command only detaches stdin (but not stdout or stderr) which implies if your command/script is running in the background but writing to stdout, you might run into a situation where Ctrl + c won't stop the output but you would be able to type in commands. This is because the background process is writing to stdout but you can't stop it because it is not running in the foreground. More info here. – vadasambar Sep 17 '19 at 14:52
6

sh is a shell. Which shell exactly depends on the system. Example for a system that uses bash as its standard shell:

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 13 16:12 /bin/sh -> bash

$ sh --version
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)

sys-snap.sh will be a shell script of some kind, so sh sys-snap.sh will execute the shell script.

& will cause the shell process to run in the background. Without & it will stay in the foreground until the script ends. The script will work either way, it's just a question of waiting for the script to end or not before executing further commands.

frostschutz
  • 48,978
1

Is your server in hostgator?

The script sys-snap.sh is a special script written by hostgator team to collect all the log files and data for the system, so they use its output in monitoring and collecting the system information.

slm
  • 369,824
tareq.t
  • 19