ESXi | Persistent Cron job | /etc/rc.local.d/local.sh writing directory to cron file - cron

I am trying to create a persistent cron job (i.e. one that survives reboot). I have followed this guide:
https://virtuallyvtrue.com/2019/03/29/shell-script-to-automatically-power-on-a-specific-vm-which-is-powered-off-vm/
I created a shell file in this directory.
/vmfs/volumes/datastore1/power-off.sh
I've updated the root cron file:
/var/spool/cron/crontabs/root
#min hour day mon dow command
1 1 * * * /sbin/tmpwatch.py
1 * * * * /sbin/auto-backup.sh ++group=host/vim/vmvisor/backup.sh
0 * * * * /usr/lib/vmware/vmksummary/log-heartbeat.py
*/5 * * * * /bin/hostd-probe.sh ++group=host/vim/vmvisor/hostd-probe/stats/sh
00 1 * * * localcli storage core device purge
*/10 * * * * /bin/crx-cli gc
00 0 * * * /vmfs/volumes/datastore1/power-off.sh
I edited the local.sh file to write the cron job and make it persistent.
/etc/rc.local.d/local.sh
Contents of local.sh
/bin/kill $(cat /var/run/crond.pid)
/bin/echo ‘00 0 * * * /vmfs/volumes/datastore1/power-off.sh’ >> /var/spool/cron/crontabs/root
crond
When the system reboots, a new cron job entry is created, but it appears to be using the time/date '*' as wildcards and is dumping the directory structure into the cron file:
#min hour day mon dow command
1 1 * * * /sbin/tmpwatch.py
1 * * * * /sbin/auto-backup.sh ++group=host/vim/vmvisor/backup.sh
0 * * * * /usr/lib/vmware/vmksummary/log-heartbeat.py
*/5 * * * * /bin/hostd-probe.sh ++group=host/vim/vmvisor/hostd-probe/stats/sh
00 1 * * * localcli storage core device purge
*/10 * * * * /bin/crx-cli gc
'00 0 altbootbank bin bootbank bootpart.gz bootpart4kn.gz dev etc include lib lib64 local.tgz local.tgz.ve locker opt proc productLocker sbin scratch store tardisks tardisks.noauto tmp usr var vmfs vmimages altbootbank bin bootbank bootpart.gz bootpart4kn.gz dev etc include lib lib64 local.tgz local.tgz.ve locker opt proc productLocker sbin scratch store tardisks tardisks.noauto tmp usr var vmfs vmimages altbootbank bin bootbank bootpart.gz bootpart4kn.gz dev etc include lib lib64 local.tgz local.tgz.ve locker opt proc productLocker sbin scratch store tardisks tardisks.noauto tmp usr var vmfs vmimages altbootbank bin bootbank bootpart.gz bootpart4kn.gz dev etc include lib lib64 local.tgz local.tgz.ve locker opt proc productLocker sbin scratch store tardisks tardisks.noauto tmp usr var vmfs vmimages /vmfs/volumes/datastore1/power-off.sh’
Are the startquote and endquote symbols required in the local.sh file? I can see that they're being inserted into the /var/spool/cron/crontabs/root file.
I'd really appreciate some help with this. I just need to power off my server at midnight and I can't believe it is this difficult.

The guide used incorrect syntax. It required " instead of ' in the /etc/rc.local.d/local.sh echo statement.
/bin/kill $(cat /var/run/crond.pid)
/bin/echo "00 0 * * * /vmfs/volumes/datastore1/power-off.sh" >> /var/spool/cron/crontabs/root
crond

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

Impossible to run .sh Script with crontab

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

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

Resources