Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I simply want to just run a command (actually its a shell script) from a given directory as a cron job. How can I run it so that it is set to a given directory without using cd.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

Your question makes little sense. Use cd. If you ask for the answer not to use cd because you've tried it and it didn't work, it's likely you didn't enter it correctly in the crontab.

The simplest way would be to write a small shell script that will cd and run your main task, then cron the script.

share|improve this answer
    
Can crontab let me put multiple commands inside one job? If not I would have to harcode the cd [path]. And path might change, thus causing really annoying deployment issues and also decentralizing configuration which is something I am avoiding at all costs. –  Zombies Jun 23 '10 at 1:30
1  
@Zombies: yes, you can put multiple commands inside one job (e.g. separating them with semicolons or &&), but you can also write a separate script that contains all the commands and just put the path to the script in cron, like Daenyth suggested. Your script could do something like reading the directory from a configuration file if you like - the point is, however you want to get the directory name into your script, it's no excuse not to use cd. –  David Z Jun 23 '10 at 1:35
    
I see. I will want to use cd on the crontab directly since if I write it in a file, I will have to edit it in each file which makes for delicate/easy-to-break configurations. –  Zombies Jun 23 '10 at 1:46
3  
And if you put the cd in the crontab file, you will have to edit it in each crontab file. I don't understand the problem. Maybe you could explain what you're trying to do more fully. –  garyjohn Jun 23 '10 at 1:51
    
And the other reason for running a shell script from the crontab file is to ensure that the environment is set correctly. The environment given to a cron job is normally barely even minimal, so you usually need to reset it to something more usable. –  Jonathan Leffler Jun 23 '10 at 3:23

Just specify the path to the script. No need for cd:

0 12 * * * /path/to/script args

If your script looks for files in ., then you will have to use cd:

0 12 * * * cd /some/dir; /path/to/script args
share|improve this answer
    
See also serverfault.com/questions/290313/… –  Will Sheppard May 23 '12 at 9:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.