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
  • 235,242

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.

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