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
.
2 Answers
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.
-
1Can 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.– ZombiesCommented Jun 23, 2010 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 usecd
.– David ZCommented Jun 23, 2010 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.– ZombiesCommented Jun 23, 2010 at 1:46
-
3And 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.– garyjohnCommented Jun 23, 2010 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. Commented Jun 23, 2010 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
-
2
-
5
&&
, as in the answer @WillSheppard links to, would be better than;
because&&
only runs the next command if the previous command was successful. Commented Nov 22, 2016 at 23:01