user714852
user714852

Reputation: 2154

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

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:

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?

Upvotes: 3

Views: 9994

Answers (3)

Ghassan Zein
Ghassan Zein

Reputation: 4189

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.

Upvotes: 1

gino pilotino
gino pilotino

Reputation: 860

I would try this one:

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

Upvotes: 0

Will
Will

Reputation: 24699

Try it like this, using a subshell, so you don't change rc.local's working dir:

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

Upvotes: 2

Related Questions