简体   繁体   中英

Start subscript in bash script in background so that it still runs when I close the terminal/ssh session

I am trying to write a simple bash script that should start an other script as another user and in a way that the script

  1. Still runs, when i close the main script
  2. Still runs, when I close the terminal or the ssh session
  3. can be stopped by a simple call of another script.

What I have right now:

This is basically how start.sh looks like

# doing some other stuff
sudo su user_the_script_should_start_as -c "./start-in-background.sh $1 $2 $3"

start-in-background-sh

# doing some other stuff
# Start other Script in Background

How can I do those three points?

You can use

nohup bash your_script.sh &

or

disown bash your_script.sh &

or

screen

But my suggestion is screen command. Also tumx is good to use.

And for your last requirement to stop script on call will be able to achieved by following command,

kill $!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM