Linux Shell Program - daemon 1
Linux 下的 Shell 编程之后台执行。
vscode 在 launch.json
中如何预启动 proxy daemon sh 脚本?
Android Studio Before launch
如何启动 proxy daemon sh 脚本?
sh#
可以新开 xterm -e
或通过 screen
执行脚本。
How to have terminal open when running shell script?
Run a shell script in new terminal from current terminal
How can I make a script that opens terminal windows and executes commands in them?
How to Run Linux Commands in Background
# suppress the stdout and stderr messages
# redirect stdout to /dev/null and stderr to stdout
command > /dev/null 2>&1 &
disown
: remove the job from the shell's job control
nohup
stands for no hangup, ignores all hangup signals(SIGHUP).
SIGHUP
is a signal that is sent to a process when its controlling terminal is closed.
If you log out or close the terminal, the process is not terminated.
Screen or GNU Screen is a terminal multiplexer program that allows you to start a screen session and open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.
Tmux is a modern alternative to GNU screen. With Tmux, you can also create a session and open multiple windows inside that session. Tmux sessions are persistent, which means that programs running in Tmux continue to run even if you close the terminal.
How to Run Linux Shell Command / Script in Background
the command will still print the output to STDOUT
or STDERR
which will prevent you from executing other commands on the terminal.
A better approach is to redirect the command to /dev/null
and later append the ampersand sign at the end as shown
Another way you can run a command in the background is using the nohup
command.
The nohup
command, short for no hang up, is a command that keeps a process running even after exiting the shell.
It does this by blocking the processes from receiving a SIGHUP (Signal Hang UP) signal which is a signal that is typically sent to a process when it exits the terminal.
To send a command or script in the background and keep it running, use the syntax:
How to run a shell script in background?
just add a &
to the end of the command
If you are running it in a terminal, and you want to then close the terminal, use nohup
or disown
If you want the script to remain after closing the terminal, another option is to use setsid
:
For more information about the differences between nohup, disown, & and setsid: Difference between nohup, disown and &.
you can just switch screen
and run your script on that 2nd screen. When script started on 2nd, switch back to 1st and do whatever you want. 2nd screen will be in the background as extra "terminal window". and it will not stop processing even when you close your ssh connection while beeing at 1st screen.
How to run a command in the background and get no output?
Use nohup if your background job takes a long time to finish.
Redirect the stdout and stderr to /dev/null
to ignore the output.
Redirect the output to a file like this:
# redirect both stdout and stderr to the same file
./a.sh > somefile 2>&1 &
# redirect stdout and stderr to two different files
./a.sh > stdoutfile 2> stderrfile &
You can use /dev/null
as one or both of the files if you don't care about the stdout and/or stderr.
redirects output to null and keeps screen clear:
Run in a subshell to remove notifications and close STDOUT and STDERR:
If they are in the same directory as your script that contains:
- The
&
at the end is what makes your script run in the background. - The
> /dev/null 2>&1
part is not necessary - it redirects the stdout and stderr streams, so you don't have to see them on the terminal, which you may want to do for noisy scripts with lots of output.
If you want to run the script in a linux kickstart you have to run as below .
These examples work fine, it works in background and does not show any output.
scpt#
macOS AppleScript 脚本后缀:.scpt
, .applescript
。
How can Apple Script open a shell script with options in a Terminal window?
do shell script without waiting
how to call a applescript/shell script without waiting for it
How to run multiple shell scripts from applescript in background
I want to start a background server process; how do I make do shell script not wait until the command completes?
Use do shell script "command &> file_path &". do shell script will return immediately with no result and your AppleScript script will be running in parallel with your shell script. The shell script's output will go into file_path
; if you don't care about the output, use /dev/null
. There is no direct support for getting or manipulating the background process from AppleScript, but see the next question.
do shell script "/bin/blah &> /dev/null &"
do shell script "/bin/blah > /dev/null 2>&1 &"
do shell script "/bin/blah &> /dev/null & echo $!" # dump process ID
The AppleScript code to run two commands in the background would look like this:
set pid1 to do shell script command1 & " &> /dev/null & echo $!"
set pid2 to do shell script command2 & " &> /dev/null & echo $!"
The pid1
and pid2
variables will be set to the process ids of the two commands.
You can later check whether the commands are still running by calling a function like this one:
on isProcessRunning(pid)
try
do shell script "kill -0 " & pid
set isRunning to true
on error
set isRunning to false
end try
return isRunning
end isProcessRunning