0

I have created a script

#!/bin/bash
 echo "tredor"

If I run it using command line, it works, but when I move it to being a cron job, it doesn't.

In crontab -e , I entered the following code

*/5 * * * *  root ./my_script

I expect to see an output "tredor" every 5 minutes but that is no happening. How can I modify my codes to be able to see output?

Seth
  • 58,122
sosytee
  • 2,698
  • 8
  • 34
  • 45
  • 1
    By using crontab -e and placing root as user in your crontbab will not probably work. Additionally you don't provide the full path to the script, except if you have expanded the path in crontab's file. You can check this question and it's answers here for some cron gotchas. Possible duplicate of http://askubuntu.com/q/337204/12218? – Stef K Aug 26 '13 at 12:08

1 Answers1

2
  • Use the full path to your script in the crontab
  • make sure it is executable: sudo chmod +x my_script
  • You got the path to the interpreter wrong in the first line of your script: it should be #!/bin/bash
  • As steeldriver pointed out, you won't actually see any output
  • Since you used 7 columns for your cronjob, I assumed you used a system wide crontab (in the /etc/cron* dirs or /etc/crontab. Using crontab -e edits your user specific crontab!
phoibos
  • 21,456
  • On using the full path, it is in the root directory so isnt the availed data enough? – sosytee Aug 26 '13 at 11:55
  • 4
    All good points - you might want to add that the echo will not show up in the terminal anyway, since the standard output from cron jobs doesn't go there (by default, it is mailed to the invoking user iirc - if a suitable mail transfer agent is available). Otherwise output needs to be redirected to a file, either within the script or within the crontab e.g. /path/to/myscript >> /path/to/logfile. – steeldriver Aug 26 '13 at 12:00
  • @steeldriver has hit the main point, I should think. – simon Aug 26 '13 at 12:18
  • I dont understand what is the problem in first line with #!/bin/bash ? I have probably the same problem. I dont see any difference. – Čamo May 13 '20 at 08:36
  • But we could run notify-send, right? @steeldriver –  Feb 20 '22 at 09:29
  • @Minsky sure - provided that cron has a way to access the graphical interface (via the DISPLAY environment variable for example) – steeldriver Feb 20 '22 at 12:43