Welcome to the Linux Foundation Forum!

Lab 15.6 Scheduling a Periodic Task with cron (need help understanding)

Hello,

Note: I am running Ubuntu 18.04.6 LTS as a VM

I need help understanding what I'm missing in order to complete this lab.

I ran two versions to test:

Version 1:- As instructed in the lab (only with exception that I created myjob.sh script on /home/amar/Desktop/ instead of /tmp/). However, nothing is printed to the terminal on scheduled time. Screenshot provided for reference

Version 2:- Similar to as instructed in the lab (with exception that myjob_v2.sh would write stdout to a file on Desktop). However, no file is created on scheduled time. Screenshot provided for reference

Please provide clarification in understanding what I'm missing.

Thank you,
Amar

Best Answers

  • luisviveropena
    luisviveropena Posts: 1,149
    Answer ✓

    Hi @cloud4amar,

    1.- If the task is wide for all the system and/or users, so it makes sense to edit/work with /etc/crontab . You can use 'crontab -e' in that case, so it checks for the syntax.

    2.- If the task is just for one user, you should use the user account to setup its own crontab. After all, who knows what the user is going to put in the crontab. So it's a matter of local security as well.

    Regards,
    Luis.

  • cloud4amar
    cloud4amar Posts: 19
    Answer ✓

    Hello @luisviveropena,

    Thank you for bringing those points to further clarify my understanding.

    In addition, upon further careful look at the man page of crontab, I figured the issue was with mycrontab file. It did not end with a newline character. Now crontab is working as expected.

    Screenshot reference:

    Thank you for the support,
    Amarpreet (amar)

  • luisviveropena
    luisviveropena Posts: 1,149
    Answer ✓

    Hi @cloud4amar, as I mentioned, it makes sense when the crontab is needed to run for all the users, or for a system task. But if it's a job that a non privileged user, the best thing is to configure it in the user's crontab.

    Regards,
    Luis.

Answers

  • Hi @cloud4amar,

    Yeah, the message is not printing to the terminal, but you can modify the script a bit, so you can see this is working.

    So, I tested it on Ubuntu 20.04.4 and I did the following:

    1.- Modified the "myjob.sh" script (on /tmp/) to the following, so it will write to a file each time it runs:

    !/bin/bash

    echo Hello I am running $0 at $(date) >> /tmp/hi.txt

    2.- Modified the crontab to it runs in a specific minute of your convenience, so you don't have to wait until it's 10 am to run:

    crontab -l
    48 * * * * /tmp/myjob.sh

    So I put '48' to run it on minute 48. You can adjust it to your needs/time.

    3.- When it ran, I took a look to /tmp/hi.txt:

    Hello I am running /tmp/myjob.sh at Ach Sak 26 11:48:01 -05 2022

    So it's working :)

    Note: on my distro the crontab job on the system is in the following path:

    /var/spool/cron/crontabs/luis

    You need to take a look at it with the user you are working.

    I hope that helps :)

    Regards,
    Luis.

  • Hello @luisviveropena,

    I followed the lab instructions again with suggested modifications, however still no success.

    Eventually, what worked for me is to edit the /etc/crontab file as root.

    I believe it's still a valid solution to schedule tasks, right? Please provide feedback.

    Here's the screenshot for reference:

    Happy Holidays!

    Thank you,
    Amarpreet (amar)

Categories

Upcoming Training