Ubuntu: How to launch startup program on specific time



Question:

Flux is nice application for those working on night time, it gives you warm color temperature on night time. I've been happily enjoying using it, link for anyone interested.

Color temperature changes on Flux started on 6PM, but rather than letting Flux modify my computer display I'd prefer turn the lights on.

This behavior makes me have to close the Flux manually, which is inconvenience.

Is there any way to make Flux only:

  • Launch between 8PM to 3AM
  • Stop between 3AM to 8PM


Solution:1

First step : create the script

First let us create a script the will be run and would enable/disable flux depending on the time (I don't knot flux syntax, so adapt it for your need). You can note that here I suppose that you need to be root to run flux, but of course if you don't just run these steps as a classical user.

sudo mkdir -p /opt/flux/  sudo gedit /opt/flux/toogleFlux.sh  

and put in it something like this

#!/usr/bin/env bash    h=$(date +%H)  start=20  stop=3  export DISPLAY=:0  if [ $h -ge $start ] || [ $h -lt $stop ] ; then      # Start flux, give it any information you want, latitude...      xflux -l 45  else      # Stop flux      killall xflux  fi  

Then, make it executable :

sudo chmod +x /opt/flux/toogleFlux.sh  

Second step : run it when needed

Now, you need to run it when needed. To to so, you can use a cron that would run it at 8:05 PM and 3:05 AM. But if you do so, if you open your computer at 8:10PM, it would not run it. The first solution is to use fcron, but I don't think it's packaged on distributions, so I'll use a combinaison of cron and a file that runs when you open a session.

To run this script with cron, run the following command. If you need to run it as root, add sudo before :

crontab -e  

and then type :

05 20 * * * /opt/flux/toogleFlux.sh  05 3 * * * /opt/flux/toogleFlux.sh  

now, you need to be sure that the script will run when you open your session. Depending on your GUI, you can configure it in your system settings, or sometimes putting this line in .profile will be enough (the xhost command is usefull to let the root user connect to the graphical interface) :

xhost +local:  /opt/flux/toogleFlux.sh  

Now, run it toogleFlux.sh to enable it for the current session :

xhost +local:  /opt/flux/toogleFlux.sh  

and enjoy flux !


Solution:2

I would like to share an alternative software that you can/should check, redshift (with its indicator redshift-gtk), I prefer it over flux for some reasons, foss, its on main repos, auto adjust color temp based on location/sunset time, easy manual settins (color temp, times...) , etc.

From author's page:

redshift example

You can simply run sudo apt install redshift-gtk on your terminal, and then launch it from Unity's dash. Also the indicator (a light bulb) has itself an option to make it autostart.

redshift indicator

Hope it helps.

UPDATE: As @JacobVlijm explained in comments (thanks for that), launching from cronjob seems to be not so simple as for non-GUI apps, so I changed my answer to simply show an alternative software solution.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »