4

I would like to start on boot Slack only when I'm at work (basically, its 10:00 am till 18:00 pm on working days). I do not really want to write custom scripts, I would like to check this in more good way, for example:

here

But how? My first approach is to use cron's @reboot with date - not looks like its possible. Secondly, I've tried to search for tool, which you pass test string in crontab format and getting error code 1 or 0 back - no luck here.

Could you please point me in right direction?

1 Answers1

4

The command date +%u will output the day of the week as number. The command date +%k will output the current hour in 24h format. We can use command substitution $() and bash double brackets test [[ to create an appropriate condition:

bash -c '[[ $(date +%u) == [1-5] && $(date +%k) == 1[0-7] ]] && /path/executable'
  • [1-5] - is a regular expression that describes the working days of the week.
  • 1[0-7] - is a regexp that describes the working hours, it will cover from 10:00 to 17:59.

  • /path/executable is a synopsis for the command/application that you want to run.

  • Within the expression [[ <condition-1> && <condition-2> ]], the logical and && means that both conditions must be fulfilled.

According to the question, you can use the above command within crontab, or with Startup Applications. For couple of reasons I would prefer to use Startup Applications.

pa4080
  • 29,831
  • 1
    i think it will work, ill find out tomorrow. – Tymur Valiiev Apr 10 '18 at 19:33
  • @ТимурВалиев, it works in the command line. If there is an issue try to add sleep 15 && in front of the command - 15 seconds should be enough for the graphical environment to be completely loaded. – pa4080 Apr 10 '18 at 19:41
  • 1
    Its not working for me. Tried few days, no luck. It works in commandline, but not via "Startup Applications" – Tymur Valiiev Apr 16 '18 at 17:59
  • how to set regex for different working hour? for example 9:00 - 18:00 – Suraj Shrestha Jul 23 '21 at 14:37
  • 1
    @SurajShrestha, try with $(date +%k) =~ ^9|1[0-7]$. Here is a reference: https://stackoverflow.com/a/21112809/6543935 – pa4080 Jul 23 '21 at 17:54