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
Related
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
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
;-)
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.
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.
As part of the setup of a docker container, the following gets injected into crontab:
*/10 * * * * /opt/run.sh >> /opt/run_log.log
According to the behavior of crontab, when should the first run kick off? Should the 10 minute cycle begin instantly, or 10 minutes after this is put into crontab. Neither behavior is happening so I am trying to debug this in more depth by trying to understand the intended behavior.
This cron sandbox simulator gives you an idea:
Mins Hrs Day Mth DoW
*/10 * * * *
This run time (UTC) Sat 2016-Jan-23 0653
Forward Schedule Sat 2016-Jan-23 0700
Sat 2016-Jan-23 0710
Sat 2016-Jan-23 0720
It uses the syntax:
Every nth '0-23/n', '*/2' would be every other.
'*/1' is generally acceptable elsewhere, but is flagged here as possibly an unintended entry.
See for example "Run a cron job with Docker" (by Julien Boulay)
Let’s create a new file called “crontab” to describe our job.
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.
The following DockerFile describes all the steps to build your image
FROM ubuntu:latest
MAINTAINER docker#ekito.fr
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
Then you can build the image with
sudo docker build --rm -t ekito/cron-example .
And run it:
sudo docker run -t -i ekito/cron-example
Be patient, wait for 2 minutes and your commandline should display:
Hello world
Hello world
If you replaced the first '' by '/10', you would have to wait to the next 0 or 10 or 20 or... of the hour.