Search code examples
macoscron

Append text to a file every minute using crontab in mac os x is not working


I'm trying to run a cron command every minute in mac os. Below is the code for my .sh file named logit.sh

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/Users/myname/Desktop

printf "\nThis is a new line to your document" >> file.txt

I made the file executable with below command

sudo chmod +x /Users/myname/Desktop/logit.sh

Below is the code for my crontab. I need to run the command every minute to append the text to the text file to check whether cron is working properly.

 * * * * * sh /Users/myname/Desktop/logit.sh

I tried the below command as well, it doesn't work

* * * * * /Users/myname/Desktop/logit.sh

However, if I give the below command in the terminal, it works fine.

sh /Users/myname/Desktop/logit.sh

What am I missing here?


Solution

  • The problem was with the syntax for the crontab file. Below is the code which worked for me.

    * * * * * cd /Users/myname/Desktop && ./logit.sh