This question already has an answer here:
这个问题已经有了答案:
- How to run a shell script at startup 15 answers
- 如何在startup 15 answers中运行shell脚本
I am trying to run a simple .sh script on reboot. I would like to have this script running forever. I have this script in a folder in the user directory, in /home/user/folder/script.sh
. It runs on a simple vps, and I have access through ssh. If I run it by doing ./script.sh &
while I am in the same directory, it works perfectly. When I close the ssh connection, it stops.
我正在尝试在重新启动时运行一个简单的.sh脚本。我想让这个脚本永远运行下去。我在用户目录的文件夹中有这个脚本,在/home/ user/folder/script.sh中。它在一个简单的vps上运行,我可以通过ssh访问。如果我执行。/脚本。当我在同一个目录下时,它工作得很好。当我关闭ssh连接时,它就停止了。
I tried to add a crontab entry like this one: @reboot /home/user/folder/script.sh &
(as suggested in other answers) and then just type sudo reboot
but it doesn't work.
我尝试添加一个crontab条目:@ restart /home/ user/folder/scriptp。sh &(如其他答案所示),然后输入sudo重启,但它不起作用。
I don't know which one is the best solution to this problem. The script.sh is this one (I found it while searching for some solution to restart the python start.py if it crashes):
我不知道哪个是解决这个问题的最好办法。脚本。sh就是这个(我在寻找重启python启动的解决方案时找到的)。py如果崩溃):
until ./start.py; do
echo "Server 'myserver' crashed with exit code $?. Respawning.." >&2
sleep 1
done
And it does exactly what I need.
它正好满足我的需要。
Thank you for your help!
谢谢你的帮助!
1 个解决方案
#1
2
When you close the ssh connection, your script gets a hang-up signal and exits. You can ignore the hang-up signal by using nohup
if you start your script like this:
当您关闭ssh连接时,您的脚本将获得一个挂起信号并退出。如果你像这样开始你的脚本,你可以使用nohup来忽略挂起的信号:
nohup ./script.sh &
Then the script will keep running after you close the connection.
然后,在您关闭连接之后,脚本将继续运行。
#1
2
When you close the ssh connection, your script gets a hang-up signal and exits. You can ignore the hang-up signal by using nohup
if you start your script like this:
当您关闭ssh连接时,您的脚本将获得一个挂起信号并退出。如果你像这样开始你的脚本,你可以使用nohup来忽略挂起的信号:
nohup ./script.sh &
Then the script will keep running after you close the connection.
然后,在您关闭连接之后,脚本将继续运行。