10

I have written a code which moves .trc files from source directive to backup directive. Now I have taken time (how much time older), source path and backup path as command line arguments for this file. Now when I invoke the script from sh, it works fine. But in crontab it is not working, which left me wondering if crontab allows passing command line arguments or not. My sh command is :

sh trace_bkp.sh 2 /home/adhikarisubir/test/basic_unix /home/adhikarisubir/test_bkp

where 2 defines 2 minutes older files, next one is soruce path and the last one is target path. I set the same in crontab as:

*/5 * * * * sh /home/adhikarisubir/test/basic_unix/trace_bkp.sh 2 /home/adhikarisubir/test/basic_unix /home/adhikarisubir/test_bkp
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
Mistu4u
  • 5,132
  • 15
  • 53
  • 91
  • If you run this command: `sh /home/adhikarisubir/test/basic_unix/trace_bkp.sh 2 /home/adhikarisubir/test/basic_unix /home/adhikarisubir/test_bkp` while you are in `/root` as user `root` does it work? – Angelo Fuchs Dec 21 '14 at 20:10
  • also in which crontab to you write this? Your own (user) crontab or the system wide (root) crontab? – Angelo Fuchs Dec 21 '14 at 20:12
  • @AngeloFuchs, There is no newline. Thanks for the edit. How do I check whose crontab is this? I only use `crontab -e` and write my code in it. – Mistu4u Dec 21 '14 at 20:13
  • When you call `crontab -e` as which user do you do it? As user or as root? – Angelo Fuchs Dec 21 '14 at 20:14
  • @AngeloFuchs, As user!! Also, my other crons work this way. Also after hard-coding the values, cron ran the script fine. – Mistu4u Dec 21 '14 at 20:14

1 Answers1

15

Yes, crotab lines can get arguments as the man page says so.

Most likely something goes wrong while calling that command that resides in the change of environment from your console to the not-a-console cron environment.

Usually its best to add logging functions to your cron line to get the output of whats happening.

*/5 * * * * sh /home/adhikarisubir/test/basic_unix/trace_bkp.sh 2 /home/adhikarisubir/test/basic_unix /home/adhikarisubir/test_bkp >> /home/adhikarisubir/test/basic_unix/cron.log 2>&1

Then read that log and you will see how it goes wrong.

Community
  • 1
  • 1
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • Thank you. I found the error. It is the mising `/` in front of `home` while passing argument in crontab. – Mistu4u Dec 21 '14 at 20:24