Impossible to run .sh Script with crontab - linux

i am trying to schedule my script.sh script to run every day from monday to friday at 9:35 am. When I run my script with ./ directly in the terminal everything works fine. But when I tried to run in the crontab -e nothing worked.
Here is the list of what I tried:
- * * * * * root /bin/script.sh
- * * * * * root sh /bin/script.sh
- * * * * * root bash /bin/script.sh
- * * * * * root / bin / sh /bin/script.sh
- * * * * * /bin/script.sh
- * * * * * sh /bin/script.sh
- * * * * * bash /bin/script.sh
- * * * * * / bin / sh /bin/script.sh
I put an execution every minute just for the test.
Otherwise the final command will be something like this:
- 35 9 * * 1-5 /bin/script.sh
I must have forgotten an important step or something.
Of course I restartcron with each modification with:
- service cron restart

That should work
* * * * * /bin/bash /bin/script.sh
You can also add a shebang in the beginning of your script
#!/bin/bash
... your script here ...
This will let you execute the script directly by calling it with /bin/script.sh
The system will know that he need to start bash to execute it
You will need to get execution access right
chmod +x /bin/script.sh
This will give execution rights to anyone, check chmod man if you want to give execution access to only owner
The cron will be just like that
* * * * * /bin/script.sh
Keep in mind you will not get any output with a cron, you can still redirect stdout to a log file
* * * * * /bin/script.sh >> /var/log/myscript.log
If you use crontab -e as root, you don't need to use sudo or anything like that

Related

Crontab can only schedule for every minutes

So i want to try make a basic schedule with crontab, this is the basic script :
* * * * * date >> /home/clauds/crontab.log
* * * * * cp /home/clauds/ahmadbagas /home/clauds/bckup
* * * * * cp /home/clauds/masjayeng.txt /home/clauds/bckup
if i do it like that it works, but when i try to change to backup file for every 5 minutes or else it doesn't work:
5 * * * * date >> /home/clauds/crontab.log
2 * * * * cp /home/clauds/ahmadbagas /home/clauds/bckup
5 * * * * cp /home/clauds/masjayeng.txt /home/clauds/bckup
i try to add user in the command like this :
5 * * * * root date >> /home/clauds/crontab.log
2 * * * * root cp /home/clauds/ahmadbagas /home/clauds/bckup
5 * * * * root cp /home/clauds/masjayeng.txt /home/clauds/bckup
it's still doesn't work, and give a error message like this:
/bin/sh: root: command not found
can someone help me, why am i only can make schedule for everyminutes?
*/5 * * * * date >> /home/clauds/crontab.log
*/2 * * * * cp /home/clauds/ahmadbagas /home/clauds/bckup
*/5 * * * * cp /home/clauds/masjayeng.txt /home/clauds/bckup
The second one will be every two minutes

Cronjobs not sending any e-mails

I have some cronjobs written on several servers using CentOS7 in combination with Plesk 18.
When I check my cron log I see that the cronjobs are started.
Even when I manually start the scripts I want to use they work, but for some reason when the crontab does it, nothing is happening.
For instance some of the cronjobs I have are:
MAILTO=support#shoptrader.nl
0 0 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/remove_files.sh
0 6 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/security_htaccess/security_htaccess.sh
15 7 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/backup_personal_files.sh
30 9 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_hacks/find_hacks.sh
*/5 * * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_sql_injection/find_sql_injection.sh
15 0 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/backup_templates_data/backup_templates_data.sh
If I check my log I see something like: Jan 14 09:30:01 web14 CROND[4402]: (root) CMD (sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_sql_injection/find_sql_injection.sh)
I've notified myself with e-mails and get this response
/var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_sql_injection/find_sql_injection.sh: line 12: php: command not found.
Which doesn't make sense, because PHP is working on the server.

Linux Cronjob does not execute

I created a cronjob with the command crontab -e:
* * * * * (filename).sh
This file test.sh should be executed every minute. But it doesn't work.
I know that it is not the script because i did run bash (name of the file) it works so the crontad is the issue.
every minute:
* * * * * sciptname.sh
every 24hours (every midnight):
0 0 * * * sciptname.sh

sh with crontab is not working in Centos

I am testing an sh script with crontab which simply creates a file
#!/bin/bash
PATH=$PATH:/bin:/usr/bin
TMP_FILE=/home/hmm/AppFlatForRent/i_am_running
touch "$TMP_FILE"
Now I want to run it on crontab and have used the following but failed
18 10 * * * sh /home/hmm/AppFlatForRent/hello.sh
Also this one
18 10 * * * usr/bin/sh /home/hmm/AppFlatForRent/hello.sh
I am also trying this
23 12 * * * /usr/bin/sh /home/hmm/AppFlatForRent/hello.sh
23 12 * * * sh /home/hmm/AppFlatForRent/hello1.sh
23 12 * * * /home/hmm/AppFlatForRent/hello2.sh
Anyone knows whats the problem?
Solution:
23 12 * * * sh /home/hmm/AppFlatForRent/hello1.sh
This works!
Give the full path to the shell from cron starting with '/':
18 10 * * * /usr/bin/sh /home/hmm/AppFlatForRent/hello.sh
Or just leave the shell out and run the script directly if it is executable:
18 10 * * * /home/hmm/AppFlatForRent/hello.sh

multiple crontab jobs but only one of them should not send email

I know that crontab job emails output of the job to its user. In my crontab file I have multiple jobs like:
10 21 * * * test1.sh
13 21 * * * test2.sh
0 * * * * test3.sh
I don't want to receive email for test3.sh. Does below code works? I want to make sure that only for the last job I wont receive email.
10 21 * * * test1.sh
13 21 * * * test2.sh
MAILTO=""
0 * * * * test3.sh
See http://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/
You could use something like
0 * * * * test3.sh >/dev/null 2>&1
There will be no output --> no mail sent.

Resources