Bob's picture

Hi everyone,

I had to reinstall my WordPress TurnKey.

I see in the Webmin webinterface the Cron job like for exemple :

How can I view/edit this cron job ? see what everyline does and how remove a line ? Thank you

Forum: 
Jeremy Davis's picture

I'm not clear how (or even if) Webmin allows you to adjust those individual jobs. But it's super easy from the commandline! There are a number of relatively straight forward commandline tools that will allow you to view and manipulate cron jobs. :)

As you've possibly already guessed, cron jobs that run hourly, daily, weekly or monthly are found in a relevant /etc/cron.{timeframe}/ directory. Whether or not they run, depends on whether they are executable or not (denoted by the 'x' bit being set, or not). So to view all the daily jobs (both those that will run and those that are disabled) you can use ls (tool to list directories and their contents). The -l switch shows the files in "long list format", which is helpful in this case. It's also worth noting that by default on TurnKey the files are color coded, which makes finding executables (in the case of cron; enabled jobs) extra easy (they're in green). E.g. on my laptop (running Debian Stretch; the basis of TurnKey v15.x):

ls -l /etc/cron/daily

Returns (note I adjusted the colors a little for improved readability on a light background):

total 64
-rwxr-xr-x 1 root root   311 Dec 29  2014 0anacron
-rwxr-xr-x 1 root root  1474 Jul 14  2017 apt-compat
-rwxr-xr-x 1 root root   355 Jun 11  2012 bsdmainutils
-rwxr-xr-x 1 root root   532 Mar 19  2015 debsums
-rwxr-xr-x 1 root root  1597 Nov 26  2015 dpkg
-rwxr-xr-x 1 root root   205 Jul 18  2016 etckeeper
lrwxrwxrwx 1 root root    37 Jun 18 10:38 google-chrome -> /opt/google/chrome/cron/google-chrome
-rwxr-xr-x 1 root root 14185 Jul 11 04:35 keybase
-rwxr-xr-x 1 root root    89 May 17  2012 logrotate
-rwxr-xr-x 1 root root  1065 Dec 14  2016 man-db
-rwxr-xr-x 1 root root   249 May 26  2012 passwd
-rw-r--r-- 1 root root   460 Oct 23  2014 polipo
-rwxr-xr-x 1 root root   550 Nov 15  2013 tklbam-backup

All the jobs in green (and the google-chrome one in cyan in my case; which is a symlink to an executable cron job file elsewhere) are "enabled" and will run whenever cron runs it's daily jobs (IIRC by default, it's usually 4-6am but can be adjusted). The job not colored ('polipo' in my case) is disabled.

To adjust whether a cron job will run or not, simply adjust the executable bit (the 'x's in the far left column). To do that, use chmod. To disable a cron job, remove the executable bit (-x), to enable a cron job, add the executable bit ('+x'). For example to disable, then re-enable my keybase cron job:

# disable - note the '-'
chmod -x /etc/cron.daily/keybase
# enable - note the '+'
chmod +x /etc/cron.daily/keybase

Generally cron jobs have (or at least should have) relatively useful names. E.g. 'tklbam-backup' is the automated daily backup job for tklbam, 'logrotate' is the daily cron job that rotates logs, etc. Although obviously how useful that is will depend on how familiar you are with your system and the software installed. Google might assist if you're unsure.

To view the contents of a file (e.g. to see what a particular cron job does), use cat like this:

cat /etc/cron.daily/tklbam-backup
#!/bin/sh

set ${JITTER:=3600}

randomsleep() {
    MAXSLEEP=$1

    if [ -n "$MAXSLEEP" ] ; then
        if [ $MAXSLEEP -gt 0 ] ; then
            if [ -z "$RANDOM" ] ; then
                # A fix for shells that do not have this bash feature.
                RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
            fi
            TIME=$(($RANDOM % $MAXSLEEP))
            sleep $TIME
        fi
    fi
}

# prevent everyone from hitting the Hub API at exactly the same time...
randomsleep $JITTER
tklbam-backup --quiet

In this case, it's a simple shell script which sleeps for a random time and does a tklbam-backup. Note that as cron runs in a limited environment, all these scripts should be shell scripts (i.e. have a #!/bin/sh shebang on the first line). Because of the limited cron environment, it's often a good idea to provide the full path to the executable you plan to run (although as per the tklbam example, that isn't required if you know that it will be in the cron system path).

Unless it's a custom cron job that you created, or you know exactly what you are doing, editing cron jobs is generally not recommended. However, if you do wish to edit one, then you can use your preferred commandline text editor. Personally I prefer 'vim' but 'nano' is probably more intuitive for Windows users. (vim users please note, on TurnKey you'll probably want to install "proper" vim, rather than use vim-tiny which is installed by default). To edit a file with nano (or substitute your preferred text editor):

nano /etc/cron.daily/my-custom-cron

If you wish to change the frequency of a cron job, simply move it to the relevant directory. E.g. to run TKLBAM hourly rather than daily:

mv /etc/cron.daily/tklbam-backup /etc/cron.hourly/tklbam-backup

If you need more fine grained control of when a cron job runs, you'll need to go a different path. But I think I'll leave it there in the hope I haven't already overwhelmed you with commandline awesomeness! :)

If you have questions and/or would like me to elaborate on anything, please ask. Feel free to give specific examples of what you're trying to achieve.

Add new comment