1

my cronjob look like this:

0 3 * * 0-6 somedir/promotionscript

But promotionscript is actually calling another file "config" located in the parent folder of somedir.

#promotionscript
#!/bin/csh
source ../config

But I got config: No such file or directory. So I suppose that cronjob is not running at somedir so it does not know somedir once start to run promotionscript. How to make cronjob locate config in the parent folder.

Note that one possible solution is that I can add full path to config:

source fullpath/config

But is there a way for cronjob to recognize the full path directly?

1

It is true using cd somedir, but instead of adding it to promotionscript, it should be added to cronjob to prevent hardcode, like:

0 3 * * 0-6 cd somedir;promotionscript

And .. operator will act as expected in promotionscript.

2

Get the dirname from the name of the shell or shell script( $0). Then do:

source `dirname $0`/../config
  • really just a good practice for writing scripts, because otherwise the script only works if run in ./somedir. It's not unreasonable for a script to assume that files are in a relative location, but it should still run no matter where you're working from generally. – Centimane Jun 8 '16 at 11:13
1

Cron runs commands in your home directory. You may add cd somedir in your promotionscript.

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.