I'm new to Linux and I've been struggling with this issue for a while in my Raspberry Pi and had no success.
First I wrote a simple script in /home/myfile.sh
like this:
#!/bin/bash
clear
echo "hi"
Then I did the sudo chmod 755 /home/myfile.sh
to grant the permissions.
And finally I modified the crontab
using crontab -e
:
# some comments ...
* * * * * /home/myfile.sh
The problem:
When I run the script manually it works fine but when I set the above line in my crontab
, nothing ever happens. What am I doing wrong?
One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.
Check permissionsAnother user might have created the cron job and you may not be authorized to execute it. Note that the root must own jobs added as files in a /etc/cron. */ directory. That means that if you don't have root permissions, you might not be able to access the job.
In order to show how I managed to solve my issue with the hope of helping others, Here I post it as an answer to my own question:
I got around the problem by using system-wide crontab (/etc/crontab
) instead of per user crontab (crontab -e
).
To clarify this,
/etc/crontab
is the system-wide crontab:
# m h dom mon dow user command
* * * * * someuser echo 'foo'
while crontab -e
is per user 'crontab':
# m h dom mon dow command
* * * * * echo 'foo'
Notice in a per user crontab there is no 'user' field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With