ubuntu docker: cron not working!

Currently reading
ubuntu docker: cron not working!

78
11
NAS
DS412+
Gents,

I try to setup a scheduled task within my Ubuntu container, not working at all:

- crontab -e
* * * * * root env > /tmp/env.output

  • Yes! cron service start I can see it using top or pgrep cron, time& date is correct same as host.
  • I checked all this Q&A at askubuntu, not working;
  • under /etc, 5 folders can be found : i.e. cron.d/; cron.daily/; cron.hourly/; cron.monthly/; cron.weekly; and a file crontab
what else shoud l check and debug?


thanks in advance for any hints
 
If you wouldn't mind sharing your Dockerfile and the command or compose.yml you used to create the container, I could give a look..
 
78
11
NAS
DS412+
thanks for help.

I have installed Utuntu standard from Synology Docker lineup, after that (apt-get) installed some required packages (e.g. cron), please let me where to find compose.yml or how to generate?
 
Last edited:
Ah, you just pulled the image and did things manually.

Typicaly an os run in a docker container is merly a bunch of loose binaries and config files, that - due to the absense of systemd (or any other supervisor daemons) - do nothing by their own. The behavior of the os in a container is not even remotely close to the behavior of the same os on bare metal or in a vm.

Commands are rarely executed directly in the shell of a docker container. Instead people write Dockerfiles to perform a scripted (as in automated) setup of the main application and its dependencies, along with one or more clever entrypoint scripts that use environment-variables or mounted files to generate the runtime configuration before the main application is started.

Here is a simple example how such a Dockerfile could look like: Ekito/docker-cron
This one has no clever entrypoint, though...
 
Last edited:
78
11
NAS
DS412+
not working, even very simple test file, e.g.
* * * * * touch /tmp/cronworks
Never seen any file generated.

I think you are right spot-on that Docker's container(e.g. ubuntu) as discrete LEGO system(image) missing integrity/completeness of the functionalities,hence the lean & base system is only to work for certain task-oriented script, once the tasks are too versatile, it will miss dependencies or framework and not function. Thus it is the user's responsibility to add or tailor-making for the designated job.

cron in docker container of ubuntu is hereby a typical example:
- the initial Ubuntu container not incl. cron even;
- ubuntu has only one user, i.e. root, which confuses cron additionally;
- No syslog or cron.log to debug or to trace,

even I tried to instal: apt-get -y install rsyslog
it throws me warnings:
...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.

also need to know how to restart cron service automatically if ubutu container reboot.
 
Didn't the example for Github work for you?

The solution is quite straight forward:


The example crontab file:
Code:
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required to run the cron job
Add the local file 'crontab' into the container and make it executable:
Code:
# 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
Precondition for CMD :
Code:
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
Start chron on container start and tail the logfile to STDOUT:
Code:
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
 
78
11
NAS
DS412+
you mean to solve the cron issue, I have to build my own new container/image using the Dockerfile?

Could we avoid re-inventing the wheel? :)
----------
even though I generated the Dockerfile and creating a new one, still not through:

$ sudo docker - < Dockerfile
Sending build context to Docker daemon 2.048kB
Step 1/8 : FROM ubuntu:latest
---> 7698f282e524
Step 2/8 : MAINTAINER [EMAIL][email protected][/EMAIL]
---> Running in 8ec7076a835d
Removing intermediate container 8ec7076a835d
---> e54192f946cd
Step 3/8 : ADD crontab /etc/cron.d/hello-cron
ADD failed: stat /volume1/@docker/tmp/docker-builder744673875/crontab: no such file or directory
 
The ADD declaration is used to copy files from the host into the container.
I assume you didn't take a look at the git repo. You need to create a file called crontab and paste the content I provided earlier.
 
78
11
NAS
DS412+
The ADD declaration is used to copy files from the host into the container.
I assume you didn't take a look at the git repo. You need to create a file called crontab and paste the content I provided earlier.
Pls. correct me if mistaken:
I have to 1:1 copied the content of Ekito/docker-cron to Dockerfile;
The dockerfile is to generate a new container/image in SynoNAS.
the build failed, e.g. ADD crontab /etc/cron.d/hello-cron
But SynoNAS/DSM has no crontab, on the other side, the container has not been built, thus also cannot add that file.

Referring to the error log above:
ADD failed: stat /volume1/@docker/tmp/docker-builder744673875/crontab: no such file or directory
you may know /volume1/@docker is not accessible...
---
Still frustrated and don't know why my via crontab -e setup-ed schedule in ubuntu container not working and no syslog coming out.
 
78
11
NAS
DS412+
I guess the problem is:

invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.

strangely I noticed if ubuntu is in bridge mode than OK in host is NOK.
 
It is not only the Dockerfile which is required, the additional file called "crontab" from the repository is also required and needs to be in the same folder.

You observation with the network mode is odd, it shouldn't affect the chron binary at all.
 
78
11
NAS
DS412+
@one-eyed-king:
Thanks for your persistent standby helping. But I am afraid we are still not touching the root cause.

Currently I am studying the systemd for ubuntu 18.04, which should be start-up in my docker image/container. But Synology/DSM has not make it possible, instead I can use service. i.e.
[email protected]:/# systemctl status
System has not been booted with systemd as init system (PID 1). Can't operate.


But sudo service --status-all shows:
[-] cron
i.e. cron not started.

This goes to the root of Synology/DSM configuration, how its embeded Docker container starts/boots -up.

t.b.c.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Welcome to SynoForum.com!

SynoForum.com is an unofficial Synology forum for NAS owners and enthusiasts.

Registration is free, easy and fast!

Similar threads

Trending threads

Top