Crontab doesn't run my script Ubuntu

0

My script is located in /opt/restart-hlasic.sh

restart-hlasic.sh contains:

#!/bin/bash
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/
SERVER="XXX"
TOEMAIL="XXX@XXX"
FROMEMAIL="XXX@XXX"
# Line divider
DL="~~~~~~~~~~~"
# Put the email together
BODY="${DL}
`date`
${DL}
Server byl restartovan
"

echo "${BODY}" | perl -e '($_ = join "",<>) =~ s/(\t)/     /g; print;' | sendEmail -f "${FROMEMAIL}" -u "${SERVER} St$

If I run script manually, mail is sent to me, but when I restart server, crontab doesn't run this script.

crontab -e

@reboot /opt/restart-hlasic.sh

I think that this is correct, or am I wrong? Thank you for help.

MyKE

Posted 2013-04-10T19:35:30.623

Reputation: 13

1The expand command is probably a better way to expand tabs; your perl command replaces each tab with a fixed number if spaces, which is not usually what you want. Where is the sendEmail command? – Keith Thompson – 2013-04-10T19:42:39.390

#!/bin/bash is wrong, it will be executed by sh, but I don't think that's your problem. – tripleee – 2013-04-10T19:47:40.433

1You say the script is kontrola, but the cron job runs restart-hlasic. Are you confused, or are you omitting something? – tripleee – 2013-04-10T19:49:36.413

@tripleee: The command in the crontab is executed by sh, but the command is /opt/restart-hlasic.sh which has #!/bin/bash, so the command executed by sh is effectively /bin/bash /opt/restart-hlasic.sh. – Keith Thompson – 2013-04-10T23:07:11.520

Answers

0

Please post how the script start is configured with cron.

I guess you try to run it when the server is (re) started. Maybe better solution would be to put the script into /etc/rc.d/rcX.d/S99"whatever" to execute it at system start. Using cron sounds... cumbersome.

Fiisch

Posted 2013-04-10T19:35:30.623

Reputation: 161

The OP mentions in the question that crontab -e shows @reboot /opt/restart-hlasic.sh – Keith Thompson – 2013-04-10T19:43:11.610

yes, I noticed...

idea to OP: do you have correct privileges on the script file? – Fiisch – 2013-04-10T19:46:37.807

Sorry it's restart-hlasic.sh script, it contain code above what I posted and crontab -e contains: @reboot /opt/restart-hlasic.sh as I posted above. Yes it could be way via rc.d but it must be via cron. And file has chmod +x – None – 2013-04-10T19:54:02.347

You should try to determine if this is a problem with cron or something else.

Make just simple script like

#!/bin/bash touch /home/somefile plan it for the reboot the same way the restart-hlasic.sh is, reboot and look if the file is created. if it is created, your problem is probably not related to cron :) – Fiisch – 2013-04-10T20:01:13.130

@Fiisch yeah, I have made simple touch script, added to cron and after restart it made a file, so something is wrong in my script, maybe cron is too fast than other services like mail and NIC? Is there way how to slow down cron for example 30 seconds? – None – 2013-04-10T20:09:15.230

maybe cron is really too fast. you should check the rcX.d for the order in which those services start...

the easiest (and pretty dirty) solution is putting sleep ... at the beginning of your script. or i think anacron has some setting like "now + defined_time" – Fiisch – 2013-04-10T20:18:31.710

@Fiisch Crontab was really too fast, I have added sleep for 3 minutes and it works! Thank you for help... – None – 2013-04-10T20:35:29.570