There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

Cronjobs: When First Connected to Internet

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.

Pantherina ,

<span style="color:#323232;">sudo cat /etc/systemd/system/CUSTOM-networkstuff.service <<EOF
</span><span style="color:#323232;">[Unit]
</span><span style="color:#323232;">Description=Ping Wikipedia.org
</span><span style="color:#323232;">After=network-online.target
</span><span style="color:#323232;">Wants=network-online.target
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Service]
</span><span style="color:#323232;">Type=oneshot
</span><span style="color:#323232;">ExecStart=/usr/bin/ping -c 1 wikipedia.org
</span><span style="color:#323232;">EOF
</span><span style="color:#323232;">
</span><span style="color:#323232;">sudo cat /etc/systemd/system/CUSTOM-networkstuff.timer <<EOF
</span><span style="color:#323232;">[Unit]
</span><span style="color:#323232;">Description=Daily ping to Wikipedia.org
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Timer]
</span><span style="color:#323232;">OnCalendar=*-*-* *:*:00
</span><span style="color:#323232;">Persistent=true
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Install]
</span><span style="color:#323232;">WantedBy=timers.target
</span><span style="color:#323232;">EOF
</span><span style="color:#323232;">
</span><span style="color:#323232;">sudo systemctl enable --now CUSTOM-networkstuff
</span>
xcutie ,

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.

Oth ,

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.

catloaf ,

Would it be a problem to just poll every few minutes?

You could script it to only actually do anything if the downloaded data is old, and if it is, then download new.

limelight79 ,

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.

taladar ,

Instead of having it sleep 10 minutes just make it a cronjob that runs every 10 minutes and has a lockfile, much more robust that way.

Valmond ,

If it should be done say only twice a day, then your way would be completely cumbersome.

taladar ,

If it should be done only twice a day sleeping for 12h would be even more error prone and you would be even less likely to have it actually run at that time.

Also, if it should be done only twice a day you don’t really want it to run twice a day only since you can not guarrantee that you have internet exactly at the time when your timer expires, you would want to check some “last_updated” timestamp and check if it was more than 12h ago.

possiblylinux127 ,

Don’t use cron for this

bionicjoey ,

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.

bjoern_tantau ,
@bjoern_tantau@swg-empire.de avatar

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

taladar ,

network-online.target is not technically about being connected to the internet but just having a network connection.

thejevans ,
@thejevans@lemmy.ml avatar

Yeah, I needed something similar and made a new target that checks whether the system can reach a common website like google

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines