Running script crontab Mac OS X - macos

I have created a script that empties the Trash every 5 minutes. However, I am not able to run the script.
Here is the location of the script.
/Users/Ryan/Documents/trash.sh
Here is what my crontab looks like.
*/5 * * * * /Users/Ryan/Documents/trash.sh
Any ideas why it's not working? Thanks in advance.

I found the answer.
*/5 * * * * sh /Users/Ryan/Documents/trash.sh

Related

How do I activate cron job?

I edited the cron file on my Mac running catalina.
I used this code:
crontab -e
And then I added this line
* * * * * python3 ~/Documents/Scripts/script.py
My expectation is that it should run every minute but it hasn't run once.
Do i need to do something special to activate cron on this machine?
Updated my code based on the conversation in the comments to this
* * * * * /usr/local/bin/python3 ~/Documents/Scripts/script.py >>~/Documents/Scripts/script.log 2>&1

Cron Unix dude with shell

I need make a task with cron but it doesn't work.
My code cron
*/1 * * * * /home/script.sh
does not call the script.
Try to check your path to the script. Maybe it is located in user directory.
*/1 * * * * /home/user/script.sh

Cronjob doesn't run Python script even though it has execute permissions and a hashbang to full path of python. Works normally outside of the cronjob

My crons are working normally, I tested it with simple file outputs and bash scripts, but nothing I have tried works when it comes to executing my python script.
So this is what I did.
as per usual I added a shebang to my script
#! /usr/bin/python3.5
&
chmod +x myPythonScript.py
I added it to crontab -e and tested on various ways.
examples:
* * * * * /path/to/myPythonScript.py
and just to be sure I even did the following
* * * * * python /path/to/myPythonScript.py
* * * * * python3.5 /path/to/myPythonScript.py
* * * * * /usb/bin/python /path/to/myPythonScript.py
* * * * * /usb/bin/python3.5 /path/to/myPythonScript.py
and even did */1 * * * * and other time tests.
Nothing worked, and if I write all of these outside the cron, everything works perfectly fine every time. I even changed the SHELL in the crontab -e knowing that it will not make any difference, but just to be sure, pasted the script in /etc/cron.hourly, nope, tried with /usr/bin/env python instead of the usual shebang, tried every other dumb thing, but it did not work out.
Cronjobs worked perfectly when running a bash script, I even ran a bash script to check if my python script is up and running and to notify me about that in a text log, and if its not, to restart it and also notify me about that, my text log was full of restarts and that was because it never ran the python script, so the cron was working obviously, because it ran the bash script.
When running cron, you should always write your standard out/err to a file to see whats happening. Don't trust mail because your mail server may/maynot be setup.
So, give this a try
* * * * * python /path/to/myPythonScript.py 1>/tmp/myPythonScript.out 2>/tmp/myPythonScript.err
After 1 minute check to see if the file /tmp/myPythonScript.{out,err} exist. This will give you a clear direction of what is happening.
Again, ALWAYS write your standard out/err

Cron job with shell script

i am trying to run a shell script using cronjob after every 2 minutes. I opened my terminal then typed
crontab-e
once i execute this command i am writing my command
*/2 * * * * /home/test/test/test.sh
but i am getting an error as
E486: Pattern not found: 2 * * * *
please help as i am new to this and i don't know why it is happening.
If you give me any links and code on hwo to execute cronjob it would help.
As mentioned in comments, the following error:
E486: Pattern not found: 2 * * * *
Was caused because you were not editing properly. That is, you were saying crontab -e correctly and then you were entering in vi. Instead of going into the insert mode, you would directly type */2 * * * * /home/test/test/test.sh, which vi would try to perform as a command, which is not.
So what you have to do is to press i to enter in the write mode. And then, just then, write */2 * * * * /home/test/test/test.sh. Finally, save the file by saying :wq.
In case other problems occur in your cronjob, you may want to check the "Debugging crontab" section in https://stackoverflow.com/tags/crontab/info.
I would like to show it in more detail:
crontab -e
i
* * * * * cd path/to/your/project && command
ESC
:wq
ENTER
Please check the link in the correctly marked answer above for learning more about crontab.

cron job not working for xwindow

I have following line in crontab
*/1 * * * * xeyes
it does not show any xwindow but on the contrary
*/1 * * * * touch somefile.txt works fine
Tried to search on google but didnt get any specific answers!!
You have to tell cron where to find the X server if the command you run uses it.
So use: env DISPLAY=:0.0 xeyes or export DISPLAY=:0.0; xeyes.
Some cron implementations (Debian, Ubuntu, ...) allows to set enviroment simply in cron file.
DISPLAY=:0.0
# m h dom mon dow command
*/1 * * * * xeyes

Resources