简体   繁体   English

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

[英]linux - how to change directory and run a command on startup using rc.local

I am running ubunto on a box in ec2. 我在ec2的盒子上运行ubunto。 I would like to execute the following command in a specific directory each time the machine starts up: 每当计算机启动时,我想在特定目录中执行以下命令:

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. 如果我直接进入/ home / ubunto / somefolder,并运行celery命令,则操作将按预期启动。 How can I run this command each time the machine starts up? 机器每次启动时如何运行此命令?

Try it like this, using a subshell, so you don't change rc.local 's working dir: 像这样,使用子外壳程序,这样就不会更改rc.local的工作目录:

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

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. 干杯。

我会尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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