this post was submitted on 26 Feb 2024
30 points (100.0% liked)

Linux

1257 readers
50 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

Hi folks, Does anyone know how to instruct cron to carry out a command when a connection to the internet is first made after boot? I have a few jobs that only need to be done once per day and require an internet connection e.g downloading the weather forecast and my rss feeds. I'm not always connected to the internet on boot and do not have the computer switched on at the same times every day. Many thanks for any suggestions.

top 7 comments
sorted by: hot top controversial new old
[–] bjoern_tantau@swg-empire.de 24 points 8 months ago

Systemd timers are able to do this. Set it up to run daily and make it require the network-online.target.

[–] possiblylinux127@lemmy.zip 18 points 8 months ago

Don't use cron for this

[–] bionicjoey@lemmy.ca 9 points 8 months ago

Cron is basically limited to doing something on a time interval. What you could do is make it so your Cron job checks for internet access and then does the thing if it finds it.

[–] Pantherina@feddit.de 7 points 8 months ago
sudo cat /etc/systemd/system/CUSTOM-networkstuff.service <
[–] xcutie@linux.community 5 points 8 months ago

Would it not just be the easiest way to put your scripts under /etc/network/if-up.d/? Then they get run once that connection is brought up.

[–] limelight79@lemm.ee 4 points 8 months ago

Personally I'd write a script to do whatever it is you want to do, checking first whether the internet connection is working. Said script can check the internet is working, if not, sleep for 10 minute or something, and try again, perhaps giving up after a set number of tries. Said script could also check the date and time of the file downloaded and confirm it is out of date.

Then, have the script execute on bootup by adding it to the startup scripts.

[–] Oth@lemmy.zip 4 points 8 months ago* (last edited 8 months ago)

Everyone else is just telling you to do things in a way that is different, and while they are correct (you should use a unit.d/systems script for this depending on your distro), I'm going to actually answer your question since I know sometimes you just need a quick and simple way.

Depending on your version of cron, it may support special statements instead of the * * * * * notation for time.

The one you want is @reboot. Replace all entries of the schedule syntax with that, including the @, and the command will be executed only once when the system boots up.

Use that to start a script that checks for network connectivity on a loop with a sleep statement. Break the loop when you have connectivity, then execute your command, and exit the script.

Don't ignore the correct way though. You're better off executing this as a systemd (or equivalent) script. It's barely more effort, and has the benefit of some nice built in logging and integrations.