how can I continously run a unix script in backgro

2019-06-12 15:19发布

how can I continously run a script in background without using crontab. The script should run even after I logout and is acceptable if it doesen't start after system reboot.I am new to unix.

5条回答
Root(大扎)
2楼-- · 2019-06-12 15:59

As others have mentioned before, you need to use nohup to prevent the process from getting the hangup signal (hence no-h-up).

However, if you start the process in the background to begin with, as

prompt> nohup process &

that has the disadvantage of not allowing you to enter any data that may be required to get the process started off. This may be passwords/credentials or other input the process needs.

If you have that requirement, start it without the "&" at the end, enter your input and then hit Ctrl-Z to put the process to sleep. To send it to the background, type "bg" at the prompt and hit Enter.

prompt> nohup process
Enter password:
(Now press Ctrl-Z)
[1]+  Stopped                 process
prompt> bg
[1]+ process &

Now even if you log off, the process will continue to run in the background.

Alternatively if you are using bash or zsh, if you didn't start the process with nohup to begin with, and killing it and restarting is not an option then you can use the built-in disown command. First pause and background the process. And then stop hangup signals from reaching it.

prompt> process
Enter password:
(Now press Ctrl-Z)
[1]+  Stopped                 process
prompt> bg
[1]+ process &
prompt> disown -h

Note: If you've got other background jobs running, you need to provide the jobspec to only disown this specific job.

prompt> disown -h %1

Instead of [1] if you'd seen [2] when you paused and sent the process to the background, you'd say disown -h %2 instead.

查看更多
劫难
3楼-- · 2019-06-12 16:02
if [ "x$1" != "x--" ]; then
$0 -- 1> /dev/null 2> /dev/null &
exit 0
fi

This is how you can run a script as a daemon. First your script (the father) will create a copy of himself (a child) so it is considerd as a process of the father. Then the father kills itself while the child is still running. Guess what happens when you do such a thing ? The child is attached to the init process. So even if you logout, the script will still run. You can even start it without the "&" operator because you start the father which is killed a millisecond after.
You can take control over it again like any program running on your computer.

By the way it's not a real "daemon" program, it's just kind of emulation. You can't just start it at the boot (I mean really the BOOT and not the loggin) if you want to start it as you login, quite simple put it in your .xinitrc

The main advantage of this solution is that your script doesn't depend on any other programm such as "nohup" which is really bad I think.

Regards
PS : If you want some informations about what the command above does, just ask me. It's just a "parameter" thing.

查看更多
趁早两清
4楼-- · 2019-06-12 16:07

start it in background using & operator e.g.

./abc.sh & this will continue till (a) the execution is complete or (b) you kill it or (c) system reboots

查看更多
女痞
5楼-- · 2019-06-12 16:17

As well as starting it in the background, as above, you may need to use 'nohup'. This means it will carry on running, even if you close the terminal.

nohup ./abc.sh &

查看更多
时光不老,我们不散
6楼-- · 2019-06-12 16:20

There's a couple of ways of doing this but the neatest way is to use screen. This lets you create a session which lives perminently on the machine and you can simply reconnect to to check on the progress of your long running process.

If you don't wish to use screen you can use nohup this allows you to run a task like:

nohup mytask &

Your task will now run in the background and will survive a log off, however there's no way to take control of it again, unlike with screen.

查看更多
登录 后发表回答