crontab: same script is triggered only on one day - cron

I'm not a linux expert and need some support to a crontab mystery (for me).
I'd like to do a backup of my raspberry pi twice a week.
It's the same script. But only the every monday trigger (dow=1) executes.
The Friday rule (dow=5) does nothing at all - no backup saved.
I can't see why.
What's going wrong? Where can I find out what's going wrong?
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 4 * * 1 /home/pi/Backup/backup.sh > /dev/null
0 4 * * 5 /home/pi/Baclup/backup.sh > /dev/null
screenshot of crontab -e

You did everything correctly. You were just missing a second pair of eyes to see that you have a typo in the second line:
Baclup vs Backup
;-)

Related

Kill a PM2 Cron Job automatically after 72 hours?

I have a PM2 with a Cron Job that run every 5 minutes
pm2 start shoot-msg.sh --name shoot-messages -i 4 --cron "*/5 * * * *"
How can I program the PM2 to quit after 72 hours and kill the cron job ?
Meaning I don't want to do pm2 delete all by myself...
Your need is a task scheduler in Ubuntu. Use Crontab to kill the process after 72 hours.
Install crontab
sudo apt install cron
Set it to run in the background
sudo systemctl enable cron
Edit the crontab
crontab -e
Select the editor
/bin/nano <--- default
/usr/bin/vim.basic
/usr/bin/vim.tiny
/bin/ed
press ENTER to select the default nano.
The editor would show the below,
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
Scroll to the bottom of the editor and add the kill command with cron
0 */72 * * * /bin/node /bin/pm2 delete all
Just replace these values (/bin/node, /bin/pm2) by what is on the /etc/init.d/pm2-init.sh file.
Save CTRL+O and exit CTRL+X

Not able to run a python script after every 5mins using cron

I edited the crontab with -e option. Then I went to /etc/cron.d directory to create a file which would run the process. Again I edit the /etc/crontab file. But I couldn't made it run. I refered to this article from stackoverflow and did exactly same but I don't know why cron is not working for me ?
here is how my crontab looks like -
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * anikde /home/anikde/Documents/pythonProjects/python_scripts/*
* * * * * anikde python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
I have set the 1st job to run after every 5 minutes and 2nd job after every one minute. But none of jobs are running automatically. While they actually run when I command the job to run first script as bash script and the second file as python script.
I assume you are trying to automatically run below script every 5 minutes:
/home/anikde/Documents/pythonProjects/python_scripts/test/write.py
First, determine the location of your Python executable, using which python command. In below examples I assume the returned path to be /usr/bin/python.
If editing your own crontab (crontab -e) try this command:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If no user is specified, the job is run as the user that owns the crontab file, using his environment. But you can also try adding the username
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If editing root crontab (sudo crontab -e)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py

Crontab not launching script

I'm trying to run the following script through crontab every day at 12 :
#!/bin/sh
mount -t nfs 10.1.25.7:gadal /mnt/NAS_DFG
echo >> ~/Documents/Crontab_logs/logs.txt
date >> ~/Documents/Crontab_logs/logs.txt
rsync -ar /home /mnt/NAS_DFG/ >> ~/Documents/Crontab_logs/logs.txt 2>&1
unmout /mnt/NAS_DFG
As it needs to run in sudo, I added the following line to 'sudo crontab' such that I have :
someone#something:~$ sudo crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 12 * * * ~/Documents/Crontab_logs/Making_save.sh
But it does not run. I mention that just executing the script thourgh :
sudo ~/Documents/Crontab_logs/Making_save.sh
works well, except that no output of the rsync command is written in the log file.
Any ideas what's going wrong ? I think I checked the main source of mistakes, i.e. using shell, leaving an empty line at the end, etc ...
sudo crontab creates a job which runs out of the crontab of root (if you manage to configure it correctly; the syntax in root crontabs is different). When cron runs the job, $HOME (and ~ if you use a shell or scripting language with tilde expansion) will refer to the home of root etc.
You should probably simply add
0 12 * * * sudo ./Documents/Crontab_logs/Making_save.sh
to your own crontab instead.
Notice that crontab does not have tilde expansion at all (but we can rely on the fact that cron will always run out of your home directory).
... Though this will still have issues, because if the script runs under sudo and it creates new files, those files will be owned by root, and cannot be changed by your regular user account. A better solution still is to only run the actual mount and umount commands with sudo, and minimize the amount of code which runs on the privileged account, i.e. remove the sudo from your crontab and instead add it within the script to the individual commands which require it.

Getting bad minute error from crontab despite seemingly accurate syntax

Not sure what's going on as this cronjob is pretty straighforward. I want my cronjob to run every 5 minutes. Below is the script (memory.sh) in it's entirety.
Below that is the schedule it's requested to run on via the crontab.
I've replicated the crontab and run crontab memory.sh both under my username and as root, but each time I get the same error:
"/opt/memory.sh":4: bad minute
errors in crontab file, can't install.
memory.sh
#!/bin/bash
now=`date +%Y-%m-%d.%H:%M:%S`
echo "$now" >>/opt/memory.out
ps aux --sort -rss>> /opt/memory.out
echo "$now" >>/opt/memory_free.out
free -m>> /opt/memory_free.out
crontab
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command*
*/5 * * * * /opt/memory.sh
directory
crontab memory.sh installs a new crontab file. memory.sh is not a crontab, it is a script references from the crontab file. You need to pass the crontab file to the crontab command, or edit the existing file using crontab -e to add the line you indicated.

Using Cron to Reboot

I'm using a Raspberry Pi for a status display, but for whatever reason it gets incredabbly sluggish after a day or so of running so I wanted to reboot it every day so I setup a cron job to do that every morning at 8:50. But, it doesn't seem to be working. Is there anything special about using cron to do a reboot?
This is my crontab for the root user:
# m h dom mon dow command
50 8 * * * shutdown now -r >> /var/log/cron.log
0,30 * * * * date >> /var/log/cron.log
The second line works just fine, but I can't seem to get the restart command to work. It doesn't even output anything to the log.
Try using the fully specified path to shutdown. date may be in the PATH in roots cron environment, /sbin may not be looked up.
You need to edit the root user crontab
sudo crontab -e
then..
50 8 * * * reboot
Save and exit.

Resources