Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


PANiCnz

990 posts

Ultimate Geek


#289024 7-Aug-2021 16:33
Send private message

I'm trying to configure a simple cron job on Ubuntu 20.04 that will run a shell script each morning at 2am. The cron job is running, but its running at 2pm each day. 

 

This is the entry in the crontab:

 

0 2 * * * /home/panicnz/backup.sh

 

And I've check the time is correct on the server.

 

Any ideas what's going wrong?


Create new topic
  #2756569 7-Aug-2021 17:21
Send private message

Have you checked your timezone is correctly set?




-- opinions expressed by me are solely my own. ie - personal




qwerty123
147 posts

Master Geek


  #2756574 7-Aug-2021 17:43
Send private message

It seems tz is set to UTC

  #2756575 7-Aug-2021 17:45
Send private message

qwerty123: It seems tz is set to UTC


Yup which would align with what you seeing since we’re 12 hours ahead of UTC. Change the tz to NZ.




-- opinions expressed by me are solely my own. ie - personal




jlittle
188 posts

Master Geek

ID Verified
Subscriber

  #2756578 7-Aug-2021 18:14
Send private message

Bear in mind that cron jobs do not run your login scripts and so have a very bare-bones environment. Your script must set up any environment variables it uses. If it works from a login shell it might not work being run by cron. First bitten by that decades ago. On my 21.04 cron jobs get only 7 variables, and the PATH is vanilla.

You say you've checked the time on the server, did you look at /etc/localtime?

ls -l /etc/localtime

PANiCnz

990 posts

Ultimate Geek


  #2756579 7-Aug-2021 18:19
Send private message

The timezone was set incorrectly when the cron job was configured, but changed afterwards. I've delete the cronjob and recreated it with the correct timezone set. Fingers crossed that's enough.

 

Its a pretty simple script, it stops all containers, rsync's some folders to a NFS share, and restarts the containers. So far the script work as it should.


fe31nz
1232 posts

Uber Geek


  #2756634 7-Aug-2021 23:56
Send private message

These days systemd timer units can be a better option than cron jobs.  They are even more flexible and you have more control over the running environment.  As an example, I run a script that does hourly checks for sufficient free space on my system partition.  It consists of two files, a .service unit and a .timer unit.  As the .service unit is set to "enabled", it will also run the script immediately after bootup as well as when specified by the .timer unit.  If the script fails (there is insufficient free space), the OnFailure= line runs a service that emails me to tell me there is a problem.  This is generic email service that is used by a number of my systemd units.

 

root@mypvr:/etc/systemd/system# systemctl cat check-free-space
# /etc/systemd/system/check-free-space.service
[Unit]
Description=Check free space on /
After=time-sync.target
OnFailure=notify-failure@%n.service

 

[Service]
ExecStart=/bin/bash -c "/usr/local/bin/check-free-space.sh"

 

[Install]
WantedBy=multi-user.target

 

 

 

root@mypvr:/etc/systemd/system# systemctl cat check-free-space.timer
# /etc/systemd/system/check-free-space.timer
[Unit]
Description=Hourly root free space check.

 

[Timer]
OnCalendar=hourly
Persistent=true

 

[Install]
WantedBy=timers.target


Create new topic





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.