Score:4

First time crontab. Is this viable?

kr flag

I tried to do some "coding" myself and this is what I came up with:

00 02 * * 1,3,5     sudo shutdown -r
@midnight           sudo apt-get update && sleep 60 && apt-get upgrade -y
@reboot             sleep 60 && sudo netplan apply && sleep 15 && sudo omd start website

It is crucial that both the network and the website are both up and running after the reboot. Also, is the 1,3,5 part viable? Would this restart the machine on Mo, We and Fr? And would it be good/smart to schedule a restart after the update/upgrade?

jp flag
Dan
I have no affiliation with the website, but I use this site often to validate my crontab "preferences": https://crontab.guru/
cn flag
@dan one of my many bookmarks ;-)
in flag
What exactly are you trying to do? I know what each line does, but what is your objective? Why omd isn't a service? What's the point of restarting the system? Unattended upgrades upgrades your system reliably for security upgrades (or other upgrades if you set it to).
Andrej Podzimek avatar
cn flag
I would strongly suggest that you use `systemd` timers instead. Learning `crontab` in 2021 is of limited usefulness, unless it’s a computer history museum project of some sort.
Score:8
cn flag

Nope.

  • You do not use sudo in cron. use root crontab if you need access to commands needing root
  • Always use absolute paths when in cron.
  • And you should not upgrade a --production-- server unattended; you do that manually. Cool if it is a desktop or if it is a testing/staging server. My personal experience is from using CLOUD servers: anything going wrong during a reboot is fatal.

But if you really want to do this I would add the following 3 ...

  • add logging to the "apt" commands
  • add a method to mail you what was printed during the apt commands.
  • before the "apt" you really should also make a full system backup that is stored outside of the server. When the update/upgrade goes wrong and it affects your system (99 out of 100 is does not but you do not want that 1 to happen ;)) getting your system back to the last working state is more important than to fix the problem.

sleep 60 && sudo netplan apply && sleep 15 && sudo omd start website

This should not be needed. I would arrange this with a "service". That way you can create a chain of commands that wait for eachother. "sleep 60" and "sleep 15"... what if it needed to be 61 and 16? Services would fix that issue for you.

Also, is the 1,3,5 part viable? Would this restart the machine on Mo, We and Fr?

As Dan said in a comment: crontab.guru with your crontab rules inserted states "At 02:00 on Monday, Wednesday, and Friday."

If there is no reason for a reboot I would not execute it. I have servers running for over 5 years where once every while I clean out memory issues and the likes :+

raj avatar
cn flag
raj
As for mailing, it is enough that the server has an MTA installed (which should be normal for a server - at least it is in Debian, I don't know how in Ubuntu Server because i used only dekstop versions of Ubuntu). Then cron will mail automatically any output to root.
cn flag
That is likely the same on Ubuntu. I know I get mails from our servers but never had to delve into the how :D
Artur Meinild avatar
vn flag
You have to install and setup an MTA (default is `postfix`) on the server, and internal mailing works "out of the box". However, if it should send to an external mail address, this needs to be configured with DKIM etc. to prevent being flagged as untrusted and/or spam.
SomeDude avatar
kr flag
For the absolute commands: is there an easy way to find the directories where the commands are located? I tried the man page and looked through bin & sbin but I am not really sure if I can trust my own research... The apt I found in /bin for example... Thanks for the idea with the services, I will take a look at that.
Artur Meinild avatar
vn flag
Use the `which` command, i.e. `which apt`.
terdon avatar
cn flag
Actually, use `type`, not `which` since `type` will also resolve aliases, functions etc. See [Why not use "which"? What to use then?](https://unix.stackexchange.com/q/85249)
in flag
"And you should not upgrade a --production-- server unattended" security upgrades are usually not an issue, which is the default for unattended-upgrades.
Score:8
vn flag

A general comment: Instead of using sudo in the user crontab, consider putting these commands in the root crontab, so they can run without sudo.

As for your 3 crontab lines:

@midnight sudo apt-get update && sleep 60 && apt-get upgrade -y

It's perfectly viable to do apt-get update and apt-get upgrade -y in this way. (Not intended for a production server, and under the condition that you understand the implications of unattended upgrades.)

00 02 * * 1,3,5 sudo shutdown -r

I wouldn't think it's necessary to reboot 3 times a week. I myself use this very simple reboot script: (called auto-reboot.sh)

#!/bin/bash

[[ -f /var/run/reboot-required.pkgs ]] && reboot

So I run this script with cron weekly, after my upgrade and cleanup script. It only reboots if the machine needs to be rebooted (if /var/run/reboot-required.pkgs exist). (Again, this is not intended for a production server.)

@reboot sleep 60 && sudo netplan apply && sleep 15 && sudo omd start website

I don't understand why you would need to run netplan apply on reboot.

Also, a more reliable way to start a service that is depending on another service or condition (e.g. network) is to create a systemd .service to do the job. There are several tutorials for this (1, 2), that I would recommend you take a look at.

cn flag
And in case someone wants to know: we wrote these 2 independent of eachother and indeed largely wrote the same things. Made me smile :-) I can't be the only upvotee :(
Artur Meinild avatar
vn flag
Yeah same for me - generally the same message, but cover some different subtle nuances. ;-)
SomeDude avatar
kr flag
Oh wow okay thank you very much... The Problem with the machine is that it keeps freezing after about 5 days uptime. And i already had it 3 times that the network settings were off after the reboot and I had to reapply the netplan. So I thought I might just restart the PC periodically since the hardware most likely isn't capable of running 24/7.
terdon avatar
cn flag
@SomeDude that sounds like you have a serious problem somewhere. My _laptop_ is capable of running 24/7, I can't imagine your hardware cannot. Check your RAM and swap usage or ask a new question and we can help you investigate.
SomeDude avatar
kr flag
@terdon I used a decommissioned [HP Prodesk 600 G3 mini](https://support.hp.com/us-en/document/c05364047) with an i5-7500T and 8GB DDR4 which is 4 years old now... I could step it up and create a VM on our live servers but for testing purposes I used this PC. To get familiar with the software and whatnot. Is there an easy way to check if it's a hardware problem?
Artur Meinild avatar
vn flag
[Here](https://askubuntu.com/questions/1038701/how-do-i-check-system-health) is a thread with some info on hardware testing. Also, try and search for the tag [hardware-test](https://askubuntu.com/questions/tagged/hardware-test).
Score:2
in flag

I agree with everything in the other answers, but let me add this...

It is dangerous to run apt-get unattended. A handful of applications ask questions during upgrades and upgrade installs, and a few others require keyboard input if you put them in the background and will hang, even though no keyboard input is needed.

Trying to run apt-get in the background like this is a good way to end up with a system where updates are broken and have to be manually configured and resumed.

The unattended-upgrades process is suppose to automatically install critical updates without intervention.

If your machine is crashing, you need to investigate why. Perhaps run hardware diagnostics overnight. Check logs around the time of the crash. Maybe check if something is running it out of memory and causing the machine to freeze. Etc...

Also, you need to run apt autoremove occasionally or kernel updates will collect until your disk fills up.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.