0

I am currently trying to schedule a cronjob to run a .sh script file every minute.

This is my simple script:

#!/bin/sh
echo "Hello World" >> /Users/navania/crontab-scrip.log

I saved this on my Desktop and named it notify.sh. I then opened a new terminal window and entered crontab-e. This opened a new nano file where I typed:

* * * * * /Users/navania/Desktop/notify.sh

I saved and existed this nano file. What should I do next so that the cronjob successfully runs?

Steps so far:

  1. Open up terminal application and type in crontab-e.
  2. This opens up a new nano file where I type the command: * * * * * /Users/navania/Desktop/notify.sh
  3. I then hit control o to save it and then control x to save it under the name of CrontabTest.
  4. When I exit, it says that no changes made to crontab, which doesn't make sense.
  5. I then opened up the logfile, and see if it prints Hello World every minute which it doesn't.

Any help would be appreciated. Thanks!

slhck
  • 223,558
  • 70
  • 607
  • 592
  • Hi Kamil, Thanks for the reply. Yes the script is executable. I executed it using the terminal command "sh /Users/navania/Desktop/notify.sh" which resulted in "Hello World", as expected. – oofmeister27 Jul 18 '19 at 22:36
  • it says permission denied when i entered it in a terminal window. – oofmeister27 Jul 18 '19 at 22:42
  • Please make sure you list *all* the steps that you did so far, what the crontab shows when you open it with `crontab -e`, what you enter, and what it says, *exactly*, when you exit the crontab. Some screenshots would help. Also show the output of `ls -l ~/Desktop/notify.sh` and `crontab -l`. – slhck Jul 22 '19 at 15:08
  • Thanks for all the help @KamilMaciorowski. It works now! – oofmeister27 Jul 23 '19 at 04:57

2 Answers2

0

You should make the script executable:

chmod +x /Users/navania/Desktop/notify.sh

And make sure the filesystem that holds the script is not mounted with noexec. Then the command

/Users/navania/Desktop/notify.sh

should work, so should the cronjob.


Alternatively the command in the crontab should be

/bin/sh /Users/navania/Desktop/notify.sh

In this case the executable permission doesn't matter (neither does the shebang in the script).


Please note one shouldn't expect the textual output of a cronjob to appear in any terminal.


And there's this issue:

  1. I then hit control o to save it and then control x to save it under the name of CrontabTest.
  2. When I exit, it says that no changes made to crontab, which doesn't make sense.

I believe you need to save whatever temporary file gets opened, not a new file. Do not change the name while saving.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • I tried entering chmod +x /Users/navania/Desktop/notify.sh as a terminal command, but then it just goes to the next line. I then ran it by /Users/navania/Desktop/notify.sh and it printed out Hello World as expected. How do I tell if it is running every minute? – oofmeister27 Jul 19 '19 at 14:02
  • I did both of these commands and it said "grep: /var/log/syslog: No such file or directory" and "No mail for navania" – oofmeister27 Jul 21 '19 at 15:09
  • whenever i exit the crontab file it says no changes made to crontab. – oofmeister27 Jul 22 '19 at 03:33
  • Sorry about not telling you about what OS I am using. I am currently using macOS Mojave version 10.14.5. I did what wila said to do but when I open up the log file, it shows Hello World. I wait for a minute and it still doesn't print another hello world. I have added my complete steps in my original post, so that it is easier to understand. – oofmeister27 Jul 22 '19 at 14:59
  • @oofmeister27 Have you executed the `chmod` command? – slhck Jul 22 '19 at 15:09
  • yes when I do that it just goes to the next terminal line. i entered chmod +x /Users/navania/Desktop/notify.sh – oofmeister27 Jul 22 '19 at 15:19
0

Your script is most likely trying to run every minute.

Follow the steps from Kamil about changing the crontab line to include /bin/sh

Change your script a bit to output to a file instead of trying to write to the screen as the command won't run in your terminal. Instead it runs in its own process, which is why you will not see any output.

For example change your script like this:

#!/bin/sh
echo "Hello World" >> /Users/navania/crontab-scrip.log

and inspect your .log file to see if new lines are being written to it.

wila
  • 56
  • 2
  • I changed notify.sh and then I saw the .log file and it only showed one Hello World. I then waited for 5 minutes and reloaded the console window and it still shows only one. – oofmeister27 Jul 19 '19 at 14:08