0

I'm trying to run a cron job that runs a script to print a message in a terminal every minute.

The script is really basic just to test the idea:

#!/bin/bash
echo "Hello World"

This is what I have in my crontab:

* * * * * /etc/cron.hourly/script1.sh

I triple checked the script has the correct permission (755) so I don't understand why it is not working. What am I doing wrong?

  • 1
    Where are you looking for the output? Does making a file instead work? – jrtapsell Oct 10 '17 at 20:40
  • Nothing to do with the script not working, but you shouldn't put your script in /etc/cron.hourly/ otherwise it will be run hourly (and every minute as configured in your crontab) – pim Nov 10 '17 at 16:56
1

cron jobs are not associated with any terminal. The output is either thrown away or possibly to mail to the user (if that is configured).

Best to redirect the job to a file then open a terminal to read the file as it is written as in:

#!/bin/bash
echo "Hello World">/home/myname/script1.sh.out 2>&1

which will send the output and errors to a file. Better would be to send the whole script output to a file if multiple commands as in:

#!/bin/bash
exec >/home/myname/script1.sh.out 2>&1 
echo "Hello World"

or do it in the crontab as in:

* * * * * /etc/cron.hourly/script1.sh>/home/myname/script1.sh.out 2>&1 

From a terminal, you can run "tail" to display the output as it occurs:

tail -f /home/myname/script1.sh.out
  • Hi Jpezz. Thanks for all the help. It makes much more sense now. I'm still having issues for some reason. Here's my script.sh file from /cron.hourly: #!/bin/bash exec >/home/mach01/script1.sh.out 2>&1 echo "Hello World" Here is the crontab config: * * * * * /etc/cron.hourly/script.sh>/home/mach01/script1.sh.out 2>&1 btw, I renamed my script to "script.sh" – Daniel Oct 10 '17 at 23:10
  • I see no info on what your issue is now. Looks like some of your comment got chopped off. – jpezz Oct 12 '17 at 17:34
  • The issue is that the script still doesn't work. – Daniel Oct 13 '17 at 20:56
  • Was there anything in the .out file? – jpezz Oct 14 '17 at 1:18

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.