linux - 如何使用rc.local在启动时更改目录并运行命令

时间:2016-06-01 09:41:30

标签: linux bash ubuntu

我在ec2的盒子上运行ubunto。我希望每次机器启动时在特定目录中执行以下命令:

celery -A someapp.tasks --concurrency=1 --loglevel=info worker > output.log 2> errors.log

我跑了

sudo nano /etc/rc.local

并编辑文件以阅读

#!/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

但是当服务器启动时,命令不会运行。如果我直接cd到/ home / ubunto / somefolder并运行celery命令,操作将按预期启动。每次机器启动时如何运行此命令?

3 个答案:

答案 0 :(得分:2)

尝试这样,使用子shell,这样你就不会改变rc.local的工作目录:

(
  cd /home/ubuntu/somefolder 
  celery -A someapp.tasks --concurrency=1 --loglevel=info worker >output.log 2> errors.log
)
exit 0

答案 1 :(得分:1)

您可以简单地使用以下内容:

sudo vi /etc/crontab

最后添加:

@reboot  cd /home/ubuntu/somefolder/ && sh -e -c 'celery -A someapp.tasks worker -Ofair --loglevel=info > output.log > errors.log'

干杯。

答案 2 :(得分:0)

我会尝试这个:

cd /home/ubuntu/somefolder && celery -A someapp.tasks --concurrency=1 --loglevel=info worker >output.log 2> errors.log