0

I am trying to make a crontab working in my Linux Mint machine without success.

I open crontab with:

sudo crontab -e

and I edit the file this way:

34 10 * * * root /home/dario/Desktop/back_up/prova_crono.sh

or:

34 10 * * * user /home/dario/Desktop/back_up/prova_crono.sh

but no job has been performing.

The script prova_crono.sh is working if I write:

./prova_crono.sh

so I really am not able to understand where is the problem. Any suggestions?

best regards

Jens
  • 69,818
  • 15
  • 125
  • 179
dario
  • 137
  • 1
  • 3
  • 11
  • What kind of OS is this? What flavor of cron? Many crons do not have a username field (e.g. ISC cron on ubuntu). What does `man 5 crontab` tell about the fields? Does root or user get emails from cron? – Jens Mar 23 '15 at 09:47
  • Could be that when you run the job yourself it's running under your .profile, and cron is not using that profile. Seen that problem on Solaris, solution was to load the relevant profile at the start of the cron job. – OTTA Mar 23 '15 at 09:52
  • How can you tell it's not working? Have you examined log files and your mailbox for error messages? – tripleee Mar 23 '15 at 09:54
  • I instructed ./prova_crono.sh to output some files. Since the files are not created I conclude that the script has not been called. – dario Mar 23 '15 at 10:20
  • @Jens The examples are:The following lists an example of a user crontab file. # use /bin/bash to run commands, instead of the default /bin/sh SHELL=/bin/bash # mail any output to `paul', no matter whose crontab this is MAILTO=paul # # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1 # run at 2:15pm on the first of every month -- output mailed to paul 15 14 1 * * $HOME/bin/monthly – dario Mar 23 '15 at 10:31

1 Answers1

1

Okay, the problem is that your cron does not accept a username as the sixth field. Remove root so your crontab entry looks like

34 10 * * * /home/dario/Desktop/back_up/prova_crono.sh

There should be tons of emails in root's inbox sent by cron telling you something like command not found: root. I suggest you install this crontab not in root's crontab, but in your own (dario?) crontab. That way, failure emails are sent to your inbox, not root's.

In addition, 99% of users are surprised to find that the PATH inherited from cron is next to empty. You should set it near the top of your script with

 #!/bin/sh
 PATH=$(/usr/bin/getconf PATH)
 ...rest of script...

You can test the cron-readiness of your script by running it with an extremely reduced environment:

 env -i HOME=$HOME /home/dario/Desktop/back_up/prova_crono.sh

If that complains about anything, fix it before installing it in the crontab.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Thanks Jens, I'll apply immediately your suggestions. I have one more question. Inside prova_crono.sh I have some instruction that need the sudo to be applied. Can I make it working also if I install the crontab in dario? – dario Mar 23 '15 at 12:53
  • Furthermore, the command env ... is not complaining. I edited my script which is now: #!/bin/bash PATH=$(/usr/bin/getconf PATH) echo "******************************* IT WORKS **************************">> log.log echo "*********************************************************">> log.log Afterwards I run install the cronojob as you suggested, but still, nodocument log.log is created. D you know why? – dario Mar 23 '15 at 13:10
  • The sudo should work if user dario is allowed to run it without a password. As for the log files not being created, try using absolute paths like `echo works >> /home/dario/log.log`. Don't forget to check your inbox for cron mails. To test if cron mails work, put this in the crontab: `* * * * * /bin/echo hello world`. This should send an email each minute since cron mails *any* output on stdout or stderr. – Jens Mar 23 '15 at 13:32
  • Thanks to your suggestion it works fine now. The problem actually confined in the paths. Best regards to everyone – dario Mar 23 '15 at 14:40
  • Great! The way to express "thanks it works" on Stackoverflow is to *accept* the anser by clicking the green arrow to the left (which you did). You might also ponder *upvoting* by clicking the up arrow. – Jens Mar 23 '15 at 15:40
  • I really would do that but as I am new I do not have yet "15" points reputation to upvote. That is way all I could do was typing the green buttom and say thanks. – dario Mar 24 '15 at 08:17