Crontab task doesn't work when I edit crontab by vim instead of "crontab -e" on docker container ubuntu18.04 - docker

Crontab task doesn't work when I edit crontab by vim instead of "crontab -e" on docker container ubuntu18.04
step 1:
Use docker run a container, the image is ubuntu18.04 os.
step 2:
vim /var/spool/cron/crontabs/root
and Write content to root file as following:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/1 * * * * . /etc/profile; /bin/sh /test_cron/xx.sh 2>&1
step 3:
cd /
mkdir test_cron
cd test_cron
step 4:
Edit xx.sh in /test_cron/xx.sh, the content as following:
echo "cron job has start" >> /test_cron/run.log
step 5:
service cron restart
step 6:
There is no run.log in /test_cron/, that’s to say, the crontab task doesn't work. But if I using "crontab -e" to open the /var/spool/cron/crontabs/root file and don't make anything modify. Just open and close the /var/spool/cron/crontabs/root, I can see the run.log file in /test_cron/, amazing, the crontab task worked. Could you tell me the reason?

Few points,
The corntab -e makes sure certain formatting and error tracking to an extent.
crontab file should contain one empty line at the end.
There are certain permission to set to the crontab chmod 600
After completing these I could see the manual entry was working. However it is not recommended to directly edit the crontab file and the best practice is to use crontab -e
EDIT: Actually emptyline should be corrected as newline character or % as per the man page
The "sixth" field (the rest of the line) specifies the command to be
run. The entire command portion of the line, up to a newline or %
character, will be executed by /bin/sh or by the shell specified in
the SHELL variable of the cronfile. Percent-signs (%) in the command,
unless escaped with backslash (), will be changed into newline
characters, and all data after the first % will be sent to the command
as standard input.

Related

Docker container environment variable file during runtime

I have a docker image that basically schedules a cron job at a frequency defined when building the image using the below.
COPY myjobtime /etc/cron.d/myjobtime
RUN chmod 0644 /etc/cron.d/myjobtime &&\
crontab /etc/cron.d/myjobtime
CMD cron
I have the cron entry in the file myjobtime.
*/10 * * * * /usr/local/bin/sh /app/myscript.py
I would like to be able to pass the cron schedule during the runtime. Meaning, if someone wants to modify the cron schedule to a different frequency, they should be able to do that while running the container and passing an environment variable file with the new cron schedule in it. Can this be done?
The important detail is that you need to create and install the crontab file when the container starts up. I find an entrypoint wrapper script to be a useful pattern for this: set the image's ENTRYPOINT to be a shell script that does whatever first-time setup is required, then have it exec "$#" to run the image's CMD.
If your image is ultimately based on a Linux distribution based on the GNU toolset, then envsubst is a really helpful program here. It reads in a text file, expands environment variable references, and writes out the result. I'll assume you have this available; on Alpine-based images you can do similar tricks with sed(1) (though escaping around the cron schedule will become tricky).
This makes the entrypoint wrapper script something like:
#!/bin/sh
# entrypoint.sh
# Set a default schedule, if the user didn't provide one
if [ -z "$CRON_SCHEDULE" ]; then
export CRON_SCHEDULE='*/10 * * * *'
fi
# Run substitutions on the template file and inject the crontab
envsubst < /app/myjobtime.cron.tmpl | crontab
# Run the main container command
exec "$#"
Since the template isn't a "normal" crontab, it can't go in the "normal" crontab directory; putting it in the application directory is fine. That file has an environment variable reference where the schedule would go
# myjobtime.cron.tmpl
${CRON_SCHEDULE} /app/myscript.py
In your image, set the wrapper script to be the ENTRYPOINT, make sure the template file is in the right place, and leave the CMD unchanged.
# (assuming there's not a broad `COPY . .`)
COPY myjobtime.cron.tmpl .
COPY entrypoint.sh .
ENTRYPOINT ["/app/entrypoint.sh"] # must be JSON-array syntax
CMD cron # unchanged
This should allow you to override the cron schedule.
docker run -d --name hourly myappcron
docker run -d --name daily -e 'CRON_SCHEDULE=0 0 * * *' myappcron
Since the entrypoint wrapper script runs whatever command was provided, and you can override the command pretty easily, this also lets you double-check that the right schedule got set.
docker run --rm -e 'CRON_SCHEDULE=0 0 * * *' myappcron \
crontab -l # runs instead of the cron daemon

Cron task inside container from host

I am trying a cron task inside a container from host but with no luck. From the host I am adding the following line on crontab -e
* * * * * docker exec -it sample_container bash -c 'touch /selected/directory/temp$(date +%H-%M)'
But this is not working. Interestingly, when I run the command independently outside crontab it is successfully executing. Can anyone explain what am I missing here?
Note: when debugging such problems with cron, you should look for errors in your local system mails or redirect those to your real mail by adding MAILTO=yourmail#yourdomain.com on top of your crontab file.
There are 2 problems with your crontab command
TLDR; the fixed cron expression
* * * * * docker exec sample_container bash -c 'touch /selected/directory/temp$(date +\%H-\%M)'
% has a special meaning in crontab
From man -s 5 crontab
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.
So you will need to escape those % signs in your date format string
Cron does not allocate a tty
Cron does not allocate a tty whereas your are trying to use one when executing your command (i.e. the -t option to docker exec). The command will therefore fail with the error the input device is not a TTY
You do not need to go interactive (-i) nor to allocate a tty for this command to do its job anyway, so you have to drop those options to launch it from cron.

Custom shell script in crontab

I've a simple shell script that executes a docker-exec command inside a container.
The script is located in /var/www/mysite-nginx/nginx-reload.sh and permissions of this file are -rwxrwxr-x
#!/bin/sh
docker exec -it mysite_nginx nginx -s reload
If I execute this script directly from shell, it works. But if I add the script to my crontab with the following line, it doesn't work.
15 4 * * * /var/www/mysite-nginx/nginx-reload.sh
I suppose that cron doesn't execute the command, or what is wrong?
On /var/log/syslog I have:
Jul 23 15:30:01 arrubiu CRON[29511]: (sergej) CMD (/var/www/mysite-nginx/nginx-reload.sh)
[EDIT] Solved in this way: docker exec is not working in cron
The issue seems to be that docker is not found. There are two ways around:
You enter the full paths of all application in your crontab script, you can find that out using e.g. locate docker, so that it looks something like
#!/bin/sh
/usr/bin/docker exec -it mysite_nginx
/usr/bin/nginx -s reload
Alternatively, you can set the $PATH and other environment variables in the same way how they are set for a usual sh-script. To achieve that, first backup what is saved in /etc/environment, and then flush it with the currently available variables by executing:
cp /etc/environment > ~/my_etc_environment_backup
env >> /etc/environment
Related questions on SO
Where can I set environment variables that crontab will use?

See cron output via docker logs, without using an extra file

I am running "cron" in a docker container.
Every day a script is executed.
The output of this script I would like to see via "docker logs "
The process with PID 0 is the cron daemon in my container. Entrypoint starts cron in foreground:
/usr/sbin/crond -f
I understand, that I could redirect the script output to a file "path/to/logs"
07 2 * * * /data/docker/backup_webserver/backupscript.sh >> path/to/logs
and start the container as following to see the logs
"tail -f path/to/logs"
But then the file "path/to/logs" would grow during the runtime of the container.
Is there a possibility to log from crontab, directly to "docker logs" ?
Change your cron file to below
07 2 * * * /data/docker/backup_webserver/backupscript.sh > /dev/stdout
This will make sure the logs go to the container output
Alpine: No need for redirection
using the default cron utility (busybox)
Dockerfile
FROM alpine:3.7
# Setting up crontab
COPY crontab /tmp/crontab
RUN cat /tmp/crontab > /etc/crontabs/root
CMD ["crond", "-f", "-l", "2"]
crontab
* * * * * echo "Crontab is working - watchdog 1"
Centos:
Redirection to /proc/1/fd/1 inside the crontab declaration line
Dockerfile
FROM centos:7
RUN yum -y install crontabs
ADD crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN crontab /etc/cron.d/crontab
CMD ["crond", "-n"]
crontab
* * * * * echo "Crontab is working - watchdog 1" > /proc/1/fd/1
#mcfedr is correct, but it took me a while to understand it with it being a one-liner with variables and some extra code related to setting up cron.
This may be a little bit easier to read. It helped me to write it out explicitly.
# Create custom stdout and stderr named pipes
mkfifo /tmp/stdout /tmp/stderr
chmod 0666 /tmp/stdout /tmp/stderr
# Have the main Docker process tail the files to produce stdout and stderr
# for the main process that Docker will actually show in docker logs.
tail -f /tmp/stdout &
tail -f /tmp/stderr >&2 &
# Run cron
cron -f
Then, write to those pipes in your cron:
* * * * * /run.sh > /tmp/stdout 2> /tmp/stderr
fifo is the way to go, it also useful because it allows cron tasks that are not running as root to write to the output.
I am using a CMD along these lines
ENV LOG_STREAM="/tmp/stdout"
CMD ["bash", "-o", "pipefail", "-c", "mkfifo $$LOG_STREAM && chmod 777 $$LOG_STREAM && echo -e \"$$(env | sed 's/=\\(.*\\)/=\"\\1\"/')\n$$(cat /etc/cron.d/tasks)\" > /etc/cron.d/tasks && cron -f | tail -f $$LOG_STREAM"]
With the tasks in /etc/cron.d/tasks
* * * * */10 www-data echo hello >$LOG_STREAM 2>$LOG_STREAM
I also prepend the env at launch to tasks so it's visible to the tasks, as cron doesnt pass it though by itself. The sed is needed because crontab format requires env vars to be quoted - at least it requires empty vars to be quoted and fails to run tasks if you have an empty var without quotes.
You could just use a FIFO.
mkfifo path/to/logs
When processes exchanging data via the FIFO, the kernel passes all
data without writing it to the filesystem. Thus, the FIFO special has
no contents on the filesystem; the filesystem entry merely serves a
reference point so that processes can access the pipe using a in the
filesystem.
man fifo
For Debian-based images, following Dockerfile works for me (note that /etc/crontab has a slightly different format, compared to user crontab files):
FROM debian:buster-slim
RUN apt-get update \
&& apt-get install -y cron
RUN echo "* * * * * root echo 'Crontab is working - watchdog 1' > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/crontab
CMD ["cron", "-f"]

Cron job doesn't work until I re-save cron file in docker container

Im using the docker Mongo container and am attempting to take a backup via a bash script. The script, executed on its own, works fine. I can also see in syslog that cron is running but the command is not showing up. If I open my file with crontab -e, then save and quit (:wq), then restart cron, the job runs fine.
Here is the relevant section of my dockerfile:
ADD mongocron /etc/cron.d/
RUN tr -d '\015' < /etc/cron.d/mongocron > /etc/cron.d/mongocron
#RUN touch /etc/cron.d/mongocron
#RUN echo "* * * * * /db_scripts/MongoDBBackup.sh >> /db_scripts/logs/backup.log\n" > /etc/cron.d/mongocron
RUN crontab /etc/cron.d/mongocron
RUN chmod 0644 /etc/cron.d/mongocron
This is what is in the file mongocron:
* * * * * /db_scripts/MongoDBBackup.sh >> /db_scripts/logs/backup.log
This is the syslog output before resaving:
And here is a picture after:
Restarting cron on its own does not fix it. I have a feeling it has something to do with line endings, so thats why you see the commented out "echo" strategy in the dockerfile with a newline. I have also verified (before saving) that my command does show in when I crontab -l
I've faced this issue too and a missing newline in the end of file was the reason. #opHASnoNAME 's answer is right but when it starts working only after editing, new line is the reason. Your editor just adds it by default.
I was struggling with the same problem in the past.
Here is a working example, tested in the wild, taken from our DevBlog.
FROM ubuntu:latest
ADD start.sh /bin/start.sh
RUN chmod +x /bin/start.sh
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/thecron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/thecron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# start script
CMD /bin/bash /bin/start.sh
The start.sh, think you don't need the part with export of env variables..
# export all environment variables to use in cron
env | sed 's/^\(.*\)$/export \1/g' > /root/envs.sh
chmod +x /root/envs.sh
# Run the command on container startup
cron && tail -f /var/log/cron.log
The Cron file, copied while building the image.
0 1 * * * root . /root/envs.sh;/bin/backup.sh >> /var/log/cron.log 2>&1

Resources