3

I use a mobile data connection service. I only have unrestricted bandwidth between 9am and 4pm.

I wonder how I can "silence" applications such as the Dropbox applet outside that schedule.

I thought about replacing the Dropbox binary with this script:

#!/bin/bash
H=`date +"%H"`
if (($H >= 9 && $H < 16))
then
    echo "run dropbox here"
fi

I was wondering if anyone had a better idea. Namely, in my solution Dropbox will keep running after 4pm, and will fail to launch if the computer is powered before 9am. It would also be cool if no restriction was applied when on wifi.

I was wondering if anyone has any solution for systemd or cron or some such. I know those tools are incredibly powerful, but don't know anything beyound that.

(My system: XUbuntu 15.04, i.e. xfce4 and systemd)

2
  • Refer to my answer here, on askubuntu. The purpose of the script is somewhat different,but I'm sure you'll get the idea. And since dropbox is GUI app, this should work May 26, 2015 at 21:05
  • FYI, the only problem with your script is that bash is that between 01 and 09, bash sees it as an octal number. Use the test operator instead: [ $H < 09 -a $H > 16 ]
    – Otheus
    May 27, 2015 at 9:00

4 Answers 4

2

Use cron to start the daemon and to kill it. Since dropbox runs as a user, edit your cronjob as your user: crontab -e and in the editor, place:

0 9 * * * $HOME/bin/dropbox-daemon-path
59 16 * * * pkill -u "$LOGNAME" dropbox-daemon-process-name

At 9am it starts the dropbox daemon (you should provide the full path here) and at 1 minute till 5pm, it kills it (for this user).

I'd love to hear from someone with a systemd answer.

EDIT: As Gilles points out, this will not be of help if the system is powered on between 9 and 17. Again, this cronjob approach is sub-optimal, but I don't know how to use dropbox with systemd. Having said that, we try your original approach in a wrapper-script which exits if the hour is outside your boundaries:

#!/bin/bash
hour=$(date +%H)
[ $hour -lt 09 -o $hour -gt 16 ] && exit
#else
exec path-to-dropbox-daemon

Modify the crontab slightly

* 9-16 * * * $HOME/bin/dropbox-wrapper-script
59 16 * * * sleep 50; pkill -u "$LOGNAME" dropbox-daemon-process-name

To me, this isn't pretty. Every minute your script is executed by cron, leaving a few lines of logs behind. But it should be effective.

10
  • I love it. Ubuntu seems to install cron by default, so it should be trivial to do. Thursday I will test it, and then select the answer. May 26, 2015 at 15:39
  • 1
    Btw, just noticed wikipedia page on cron is outstanding - en.wikipedia.org/wiki/Cron May 26, 2015 at 15:42
  • Although cron runs the jobs in the context of the user, it does not run it within that user's typical environment. It will set HOME and SHELL, but will not set PATH. I've modified the example accordingly for an example.
    – Otheus
    May 26, 2015 at 16:04
  • That won't launch the application if the computer is booted between 9 and 4pm. @RicardoCruz May 26, 2015 at 22:34
  • 1
    @Otheus, thank you so much! This is for a nephew who lives in the countryside. I will try tomorrow, and select the answer. I had already noticed your approach did not work if he powered between 9am and 16pm - which would be his ordinary use-case. I already planned to use my script with your cron approach - but thank you for fixing it already. Please stop :) I will let you know how it works out - Thanks! May 27, 2015 at 17:51
1

Set up a cron job to run at certain hours; as @Janis mentioned, this might take the form of entries like:

0       9 * * * bin chmod +x /usr/bin/dropbox
0-59/10 * * * * root  /usr/local/bin/maybe-stop dropbox

Revised: Running it every 10 minutes as I'd initially written is mad overkill, since only a superuser could have changed it back to +x, so a better idea is maybe:

0 9  * * * bin chmod +x /usr/bin/dropbox
0 16 * * * root  /usr/local/bin/maybe-stop dropbox

Where /usr/local/bin/maybe-stop is:

#!/bin/bash
RESTRICT_PROGRAM=$1
WIFI_DEVICE=wlo1

BEGIN_FREE=9
END_FREE=16

if [ $(date +%H) -ge $BEGIN_FREE -a $(date +%H) -lt $END_FREE ]
then
    # We have free broadband: allow it.
    chmod +x /usr/bin/$RESTRICT_PROGRAM
elif ip addr show dev $WIFI_DEVICE | grep '   inet' | grep -q global
then
    # We have WiFi: allow it.
    chmod +x /usr/bin/$RESTRICT_PROGRAM
elif
else
    chmod -x /usr/bin/$RESTRICT_PROGRAM
    if [ $(date +%H) -lt $BEGIN_FREE -o $(date +%H) -ge $END_FREE ]
    then
        PIDS=$(fuser /usr/bin/$RESTRICT_PROGRAM 2>/dev/null | cut -d: -f2)
        kill -SIGTERM $PIDS
        sleep 3
        kill -SIGKILL $PIDS
    fi
fi

The second part would be to link that same script to be run when the network configuration changes:

/etc/NetworkManager/dispatcher.d/dropbox-check

#!/bin/sh
exec /usr/local/bin/maybe-stop dropbox
0

One option using cron could be to set up a cronjob at 9:00 and one at 16:00 that creates (or resp. deletes) a flag-file in a certain place. And the wrapper would check the existence of that flag-file to determine whether it will start the application or not.

If your application got started before 16:00 and still runs after 16:00, you could of course - assuming you don't know in advance how long the run lasts - kill the application (e.g. initiated by a cronjob).

1
  • If your system is not up at 16:00, the flag file will not be deleted, and your wrapper would not work as expected. And there are plenty of reason why that might occur: power outage is one example to think of.
    – jehon
    Sep 3, 2022 at 20:38
0
timeStart=`date --date='8AM' +"%s"`
timeStop=`date --date='23:59:00' +"%s"`
now=`date +"%s"`

if (($now >= $timeStart && $now < $timeStop))
then
        #CRON JOBS BETWEEN 8AM AND 12PM
        echo "NO BEER"
else
        #CRON JOBS OUTSIDE OF 8AM AND 12PM
        echo "DRINK BEER"
fi
1
  • The question has been answered more thoroughly. Your script makes no sense. Oct 3, 2015 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .