Ubuntu background running a ./sh file
- Get link
- X
- Other Apps
If you need to run your ./app.sh script in the background and ensure it continues running after you close the terminal, without manually detaching from a session as required by screen or tmux, you can combine nohup with & and then immediately exit the terminal. This approach is suitable if you're running a command via SSH and want to close the session right after starting the command, or if you're scripting the execution and termination process.
Here's how you can do it:
shnohup ./app.sh > app.log 2>&1 &
Breaking down the command:
nohupensures that the command doesn't receive the hangup signal when the terminal is closed../app.shis your script you want to run in the background.> app.logredirects standard output (stdout) to a file namedapp.log, so you won't lose your output after the terminal is closed.2>&1redirects standard error (stderr) to standard output (stdout), which means both output and errors go toapp.log.&at the end runs the command in the background.
After running this command, you can safely close the terminal, and your script will continue to run in the background. The output and any errors will be logged to app.log, so you can check this file for the output of your script.
If you want to close the terminal programmatically right after running the command (for example, when running commands in a script), you can use the exit command right after:
shnohup ./app.sh > app.log 2>&1 &
exit
This sequence will start ./app.sh in the background, then exit the terminal session immediately, leaving the script running.

If your script name is run-applio.sh and you want to run it in the background, ensuring it continues to run after you close the terminal, you can use the nohup command as follows:
shnohup ./run-applio.sh > run-applio.log 2>&1 &
After executing this command, you can immediately close the terminal, and run-applio.sh will keep running in the background. Here's a breakdown of what each part of the command does:
nohupprevents the command from being terminated when the terminal closes../run-applio.shis the command to run your script.> run-applio.logredirects the standard output (stdout) to a file namedrun-applio.log, so you won't lose any output.2>&1redirects standard error (stderr) to the same place as stdout, which means both outputs and errors will be captured inrun-applio.log.&puts the command in the background so you can continue to use the terminal or close it.
To ensure the command is executed and the terminal is closed (especially useful if you are connecting via SSH or running this in a script), you can use:
shnohup ./run-applio.sh > run-applio.log 2>&1 &
exit
This will start run-applio.sh in the background and log its output and errors to run-applio.log, then exit the terminal session, leaving the script running in the background.

To stop a background process that was started with nohup, you'll first need to find the process ID (PID) and then use the kill command to stop it. Here's how you can do it step by step:
1. Find the Process ID (PID)
First, you need to find the PID of your background process. If you know the command or a unique part of it, you can use the pgrep command or ps command combined with grep. Since your script is run-applio.sh, you can try:
shpgrep -f run-applio.sh
or
shps aux | grep run-applio.sh
- The
pgrep -f run-applio.shcommand looks for processes running therun-applio.shscript. The-foption makespgrepsearch through the full command line, not just the command name. - The
ps aux | grep run-applio.shcommand lists all running processes and filters the list to show only those that includerun-applio.sh. However, this will also show yourgrepcommand as a process, which you can ignore.
2. Stop the Process
Once you have the PID of your script, you can stop it using the kill command. If the PID was, for example, 12345, you would run:
shkill 12345
If the process does not stop (some processes ignore the standard TERM signal), you can use the kill -9 command, which sends a KILL signal to forcibly stop the process. Be cautious with this command, as it does not allow the process to clean up:
shkill -9 12345
Summary
Stopping a background process involves identifying it by its PID and then using kill to send it a signal to terminate. Use pgrep -f for a straightforward search or ps aux | grep for a more manual look, then kill (or kill -9 for a forceful stop) with the PID to stop the process.
- Get link
- X
- Other Apps
Comments
Post a Comment