Bilal Usean
Bilal Usean

Reputation: 2474

crontab not working ubuntu 16.04

I have referred 10+ links of similar issue but none of one worked. I am working in ubuntu 16.04 docker container. I am trying to set cron jobs. I referred this link to install cron.

for making demo add below line in crontab -e (as a root user)

* * * * * echo "Run this command every minute" >> /var/www/public/cronfile.log 

when saving file it will notify crontab: installing new crontab but none of file created at a specified location.

Below steps are I am trying to sort out that issues but none of one worked.

  1. change crontab SHELL=/bin/sh to SHELL=/bin/bash
  2. append 2>&1 in crontab -e

    * * * * * echo "Run this command every minute" >> /var/www/public/cronfile.log 2>&1

  3. check permission /etc/crontab (root user I am also run as a root user crontab -e)

  4. I could not found any log file in /var/log/. also var/log/syslog not found. kindly note it is a ubuntu docker container. I have tried with below command but I could not found any cron related logs.

    find / -type f -name '*.log'

note: Actually the origin of the issues come from here. If you need any additional info please mention in comment.

can anyone assist me what I am doing wrong here?


Update

I tried the same replication of @BMitch answer but that will also produce same problem. I am waiting more than five minutes but no cron log file generated in /var/www/public/cronfile.log, please see my stuff

[root@bu bu]# docker run -it --rm ubuntu
root@a256be07f23a:/# ( apt-get update && apt-get install cron ) >/install.log 2>&1
root@a256be07f23a:/# service cron start
 * Starting periodic command scheduler cron                                                              [ OK ] 
root@a256be07f23a:/# mkdir -p /var/www/public
root@a256be07f23a:/# crontab - <<EOF
> * * * * * echo "Run this command every minute" >> /var/www/public/cronfile.log
> EOF
root@a256be07f23a:/# crontab -l
* * * * * echo "Run this command every minute" >> /var/www/public/cronfile.log
root@a256be07f23a:/# date
Fri Sep  9 04:47:50 UTC 2016
root@a256be07f23a:/# date
Fri Sep  9 04:50:59 UTC 2016
root@a256be07f23a:/# ls -al /var/www/public
total 0
drwxr-xr-x 2 root root  6 Sep  9 04:47 .
drwxr-xr-x 3 root root 20 Sep  9 04:47 ..
root@a256be07f23a:/# date
Fri Sep  9 04:51:21 UTC 2016
root@a256be07f23a:/# ls -l /var/spool/cron/crontabs/
total 4
-rw------- 1 root crontab 254 Sep  9 04:47 root
root@a256be07f23a:/# 

version check

root@660f8f6d128e:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"

Upvotes: 1

Views: 2988

Answers (1)

BMitch
BMitch

Reputation: 263866

Docker is an application isolation tool, unlike how virtual machines are an OS isolation tool. That means you run your application inside of a Docker container, and in that container no other OS services components are running by default. So if you configure your crontab, you don't have the cron daemon running that would process that file and kick off the jobs. To have this run for you, a cron -f needs to be run to launch the daemon in the foreground. If your container runs another application (most likely), then you'd need a tool like supervisord to launch multiple applications to also run cron.


Edit: this is my attempt to reproduce your error in my lab after starting cron. As you can see, the file is created for me:

$ docker run -it --rm ubuntu
root@f2e78e1bacde:/# ( apt-get update && apt-get install cron ) >/install.log 2>&1
root@f2e78e1bacde:/# service cron start
 * Starting periodic command scheduler 
root@f2e78e1bacde:/# mkdir -p /var/www/public
root@f2e78e1bacde:/# crontab - <<EOF
> * * * * * echo "Run this command every minute" >> /var/www/public/cronfile.log
> EOF
root@f2e78e1bacde:/# crontab -l
* * * * * echo "Run this command every minute" >> /var/www/public/cronfile.log
root@f2e78e1bacde:/# ls -al /var/www/public
total 12
drwxr-xr-x 2 root root 4096 Sep  7 11:54 .
drwxr-xr-x 3 root root 4096 Sep  7 11:53 ..
-rw-r--r-- 1 root root   30 Sep  7 11:54 cronfile.log
root@f2e78e1bacde:/# date
Wed Sep  7 11:54:18 UTC 2016
root@f2e78e1bacde:/# ls -l /var/spool/cron/crontabs/
total 4
-rw------- 1 root crontab 255 Sep  7 11:53 root
root@f2e78e1bacde:/#

Upvotes: 2

Related Questions