bash linux - 如何使用 rc.local 在启动时更改目录和运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37564982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux - how to change directory and run a command on startup using rc.local
提问by user714852
I am running ubunto on a box in ec2. I would like to execute the following command in a specific directory each time the machine starts up:
我在 ec2 的一个盒子上运行 ubunto。我想每次机器启动时在特定目录中执行以下命令:
celery -A someapp.tasks --concurrency=1 --loglevel=info worker > output.log 2> errors.log
I've run
我跑了
sudo nano /etc/rc.local
and edited the file to read
并编辑文件以读取
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
(
cd /home/ubuntu/somefolder/
sh -e -c 'celery -A someapp.tasks worker -Ofair --loglevel=info > output.log > errors.log'
)
exit 0
But when the server starts up the command doesnt run. If I cd directly into /home/ubunto/somefolder and run the celery command the operation starts as expected. How can I run this command each time the machine starts up?
但是当服务器启动时,命令不会运行。如果我 cd 直接进入 /home/ubunto/somefolder 并运行 celery 命令,操作将按预期开始。每次机器启动时如何运行此命令?
回答by Will
Try it like this, using a subshell, so you don't change rc.local
's working dir:
像这样尝试使用子shell,这样您就不会更改rc.local
的工作目录:
(
cd /home/ubuntu/somefolder
celery -A someapp.tasks --concurrency=1 --loglevel=info worker >output.log 2> errors.log
)
exit 0
回答by Ghassan Zein
You can simply use the following:
您可以简单地使用以下内容:
sudo vi /etc/crontab
Add this at the end:
在最后添加这个:
@reboot cd /home/ubuntu/somefolder/ && sh -e -c 'celery -A someapp.tasks worker -Ofair --loglevel=info > output.log > errors.log'
Cheers.
干杯。
回答by gino pilotino
I would try this one:
我会试试这个:
cd /home/ubuntu/somefolder && celery -A someapp.tasks --concurrency=1 --loglevel=info worker >output.log 2> errors.log