Ctrontab -e does not execute commands - cron

I am not getting it but below part is taken from the sudo crontab -e but none of the commands are executed as scheduled. I tried with sudo and without (last should be correct, as it runs as root). If I execute the commands manually with sudo, all three do work.
# create SD backup every Wed at 1am:
0 1 * * 3 sudo sh /home/pi/bkup_rpimage/bkup_rpimage.sh start -c /media/pi/RaspiSDBack/rpi_$(date +%Y-%m-%d).img
# copy SD backup to NAS every 15th at 4-30 am:
30 4 15 * * sudo cp -n -r /media/pi/RaspiSDBack/* /home/pi/QNAP/PI_Backup/
# daily reboot at 4 am:
0 4 * * * sudo /sbin/shutdown -r +5
Anyone an idea? I am new to this and can't get any further, currently.

Related

crontab not being executed into a container after setting up permissions

I'm running a docker container with an image:
ubi8/ubi-minimal
The cronjob has correct path and go packet is already installed:
crontab -l
*/2 * * * * go run /usr/local/src/script.go
The file has correct permissions:
-rw-r-xr-x 1 root root 6329 Jun 16 15:10 script.go
However the crontab -e is like this:
/bin/sh: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127
and
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
The crontab was added in the dockerfile like this:
RUN crontab -l | { cat; echo "*/2 * * * * go run /usr/local/src/script.go"; } | crontab -
I think is correctly setup isn't it?
the crontab should execute the script every 2 minuts but it's not. Also the image is minimal and I cannot edit any file I just included some permissions to the files from the dockerfile.
If needed to change any Path from crontab I have to do this trough the dockerfile.
As it sounds like a lot of trouble, consider skipping the cron daemon entirely and just sleep in a loop
#!/bin/sh
while true; do
TIME_LOOP_START=$(date +%s) # integer time in seconds
script.go
# calculate offset for 2 minutes in seconds
sleep $(($TIME_LOOP_START + 120 - $(date +%s)))
done
adapted from
https://askubuntu.com/questions/852070/automatically-run-a-command-every-5-minutes
Get current time in seconds since the Epoch on Linux, Bash
You may find this is even better extended by making the time and target executable arguments $1 $2
You need to start the cron daemon. Here's a Dockerfile I made to illustrate
FROM registry.access.redhat.com/ubi8/ubi-minimal
RUN microdnf update && microdnf install cronie
RUN crontab -l | { cat; echo "*/2 * * * * /usr/local/src/script.sh"; } | crontab -
COPY script.sh /usr/local/src/
CMD crond -n
Note that the CMD runs crond with the -n option which keeps crond in the foreground. If we let it daemonize, docker would see that the process had ended and would terminate the container.
Instead of using go, I made a small shell script like this, called script.sh
#/bin/sh
echo Hello from script >> ~/log.txt
It writes to /root/log.txt every 2 minutes.

How to write a script could restart the job when exceeding a limit time in Linux?

I'm writing a script could do the same job 100 times and each in a different directory (named run1 to run100). However, the job will stuck for a long time sometimes and I have to delete the directory containing that run and restart it.
Is it possible to write a script that could 1. stop and delete the directory (e.g., run13) if that run exceeds 6 hours and 2. restart that run again?
Here is my original shell script
PREFIX=earlymigration
for i in {1..100}
do
mkdir run$i
cp ${PREFIX}.tpl ${PREFIX}.est ${PREFIX}_jointMAFpop1_0.obs run$i"/"
cd run$i
fsc26 -t ${PREFIX}.tpl -e ${PREFIX}.est -m -0 -C 10 -n 200000 -L 40 -s0 -M -c 10
cd ..
done
So do exactly that. Timeout the command, and if it times out, restart it.
while true; do
timeout $((6 * 60 * 60)) fsc26 ....
ret=$?
if ((ret == 124)); then
rm the_directory_containing_that_run
continue
fi
break
done
See man timeou.

Cron won't execute none of my commands on Ubuntu 21.10 impish

I'm trying to run a Docker container every other minute that is stopped via cron job but it seems not working.
What I've done is launch the command crontab -e and add the line
*/1 * * * * docker start sender >> /home/cronlog.log 2>&1
I've added the user group to Docker as explained here (in fact I can access docker from the terminal without sudo)
I have also tried to add the command into a script as below
*/1 * * * * /home/start_container.sh >> /home/cronlog.log 2>&1
with the script containing
#!/bin/sh
docker start sender
but still, nothing happens. The cron process is working tho as using the command ps -ef | grep cron I got
root 881 1 0 08:42 ? 00:00:00 /usr/sbin/cron -f -P
nicola 10905 10178 0 11:31 pts/0 00:00:00 grep --color=auto cron
Am I missing something? (Obviously, the commands work if launched manually from the terminal)
Try using the docker path instead.
type the following command to get the path of docker.
$ where docker
/usr/bin/docker
/bin/docker
then try any one of the paths in the cron script
*/1 * * * * /bin/docker start sender >> /home/cronlog.log 2>&1
or
*/1 * * * * /usr/bin/docker start sender >> /home/cronlog.log 2>&1
It turned out that, for some reason, the cron doesn't like the /home/ (at least, in this specific instance)
I've fixed using another path such as
*/1 * * * * docker start sender >> /tmp/cronlog.log 2>&1

How to make linux to reboot every day?

I tried to use cron but I don't see it reboots - no one program restarts.
I wrote to my crontab -e
48 8 * * * sudo reboot
I tried to make it reboot every day at 8:48. Why it doesn't work?
Sudo? Try without sudo in root crontab.
Use sudo crontab -e and include "/sbin/" on your command "reboot.
48 8 * * * /sbin/reboot

If adding a command that repeats every 10 minutes to crontab, when does the first job run?

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.

Resources