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.


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.

  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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