You are not logged in.

#1 2011-06-03 18:21:12

sweetthdevil
Member
Registered: 2009-10-20
Posts: 376

Bash Script checking time,

Hi all,

following my other topic to remove the shutdown/reboot/logout power, I now need to make a script to execute the given command at specific time.

What I am trying to do (see script below) is to get the time into a variable, then check if the time is more than the time given or not, I start working on script but the result failed.

#!/bin/bash
time=`date +%k%M` ;
if [ "1830"<"$time" ]
then
gsettings set org.gnome.desktop.lockdown disable-log-out true
elif [ "2220">"$time" ]
then
gsettings set org.gnome.desktop.lockdown disable-log-out fasle
fi

So basically the script will disable the shutdown/reboot/logout user power if after 1830 and re-enable it after 2220, obviously I will make it a loop after that - might be good to get to a point where the script check if the time is between 2220 and 1830 to enable it, or if it's between 1830 to 2220 to disable it.

Many thanks for your time and future assistance,

Regards,

Offline

#2 2011-06-03 18:30:47

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash Script checking time,

Maybe you can set up two cronjobs - one at 1830, the other at 2220.

Offline

#3 2011-06-03 18:32:18

sweetthdevil
Member
Registered: 2009-10-20
Posts: 376

Re: Bash Script checking time,

Well the issue with that is the user doesn't log at specific time, therefore the cron job could have been passed already - other option would be to set the cron job every 10/15 minutes to run that command between those time.

Not ideal I guess,

Offline

#4 2011-06-03 18:55:21

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash Script checking time,

There's e.g. http://xyne.archlinux.ca/projects/cronwhip/ - maybe it could help: if the user is logged on at 18.30, run the job; if the user is not logged in, run as soon as he logs in. Ideally don't run it after 22.20 but if the user logs off at 18.00 and logs in at 23.00 both jobs would be run - is it still OK?

Offline

#5 2011-06-03 19:11:26

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash Script checking time,

sweetthdevil wrote:

Hi all,

following my other topic to remove the shutdown/reboot/logout power, I now need to make a script to execute the given command at specific time.

What I am trying to do (see script below) is to get the time into a variable, then check if the time is more than the time given or not, I start working on script but the result failed.

#!/bin/bash
time=`date +%k%M` ;
if [ "1830"<"$time" ]
then
gsettings set org.gnome.desktop.lockdown disable-log-out true
elif [ "2220">"$time" ]
then
gsettings set org.gnome.desktop.lockdown disable-log-out fasle
fi

So basically the script will disable the shutdown/reboot/logout user power if after 1830 and re-enable it after 2220, obviously I will make it a loop after that - might be good to get to a point where the script check if the time is between 2220 and 1830 to enable it, or if it's between 1830 to 2220 to disable it.

Many thanks for your time and future assistance,

Regards,

The '<' and '>' operators inside [ ] are lexcographical, not arithmetic. 830 will be greater than 1030. Since you've specified #!/bin/bash, please use Bash's (( )) syntax, where '<' and '>' will actually perform numerical comparisons.

http://mywiki.wooledge.org/BashGuide/Te … nditionals

Last edited by falconindy (2011-06-03 19:12:41)

Offline

#6 2011-06-03 19:25:35

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Bash Script checking time,

#!/bin/bash
user=someuser
time=$(date +%k%M)

if [[ "$time" -ge 1830 ]] && [[ "$time" -le 2220 ]];then
    su -c "gsettings set org.gnome.desktop.lockdown disable-log-out true" $user
else
    su -c "gsettings set org.gnome.desktop.lockdown disable-log-out false" $user
fi

Try this. I don't know if gnome settings have to be set as a specific user so i added su.


no place like /home
github

Offline

#7 2011-06-03 21:10:04

sweetthdevil
Member
Registered: 2009-10-20
Posts: 376

Re: Bash Script checking time,

Great! many thanks,

I have changed the script because it doesn't need "su" and also because I will start the script automatically when the user log in. However, I am trying to make a loop so the script check the time and act accordingly due to the time but it doesn't seems to work (because when the time is up, the option to log out/reboot/power off doesn't return automatically)

#!/bin/bash
time=$(date +%k%M)
while true; do
if [[ "$time" -ge 1830 ]] && [[ "$time" -le 2220 ]];then
      gsettings set org.gnome.desktop.lockdown disable-log-out true
else
      gsettings set org.gnome.desktop.lockdown disable-log-out false
fi
sleep 300
done

Offline

#8 2011-06-03 22:09:23

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Bash Script checking time,

I think it's better to have cron run the script every ~5 minutes or so.
But if you want the loop inside the bash script you have to evaluate $time from inside the loop. Right now, $time is set only once when the script first starts and will stay that way.

Try this:

#!/bin/bash
time="date +%k%M"
while true; do
  if [[ $(eval "$time") -ge 1830 ]] && [[ $(eval "$time") -le 2220 ]];then
      gsettings set org.gnome.desktop.lockdown disable-log-out true
  else
      gsettings set org.gnome.desktop.lockdown disable-log-out false
  fi
  sleep 300
done

Last edited by demian (2011-06-03 22:15:30)


no place like /home
github

Offline

#9 2011-06-04 11:31:02

sweetthdevil
Member
Registered: 2009-10-20
Posts: 376

Re: Bash Script checking time,

excellent, seems to work as intended from the testing I have done,

Many thanks for your help!!

Offline

#10 2012-02-09 00:40:33

sweetthdevil
Member
Registered: 2009-10-20
Posts: 376

Re: Bash Script checking time,

Sorry to dig an old post,

But the script is now returning an error and stop working.

 line 4: 040: command not found 

Script run at 00:40 any suggestion?

Offline

#11 2012-02-09 09:26:34

Blµb
Member
Registered: 2008-02-10
Posts: 224

Re: Bash Script checking time,

You must have changed something.
Anyway, rather than sleeping and re-checking all the time, better just check *once* and then use the "at(1)" command smile


You know you're paranoid when you start thinking random letters while typing a password.
A good post about vim
Python has no multithreading.

Offline

#12 2012-02-09 10:14:14

sweetthdevil
Member
Registered: 2009-10-20
Posts: 376

Re: Bash Script checking time,

Still don't get it, but now it work.

With the "at" command it will only execute the command at that specific time, but that's not what I want. Anyway thanks for your help smile

Offline

Board footer

Powered by FluxBB