Saturday, 10 September 2016

Execute Commands at a Specific Time in Linux-"at"

The other day I was reading about Reliance Jio Offers. Most of their plans come with limited data, but during night times you get unlimited usage. The catch is that night time period is only three hours 2:00 am to 5:00 am.

Unless you are either nocturnal animal like an owl, or a bi-phasic / polyphasic sleeper this implies that you cannot use the unlimited night time benefit. While this might be the case for an average user but for expert users(people who read my blog :-P) this is no problem.

Guess what Linux offers multiple ways to solve this problem

  1. Cron : Read about this yourself. I will not be covering this in the current article. This is more suitable for recurring jobs.
  2. At: The at command this is more suitable for one time jobs. This will be covered in this article.
Android also offers this facility. You might need to have root access for this though. I have covered this in one of earlier articles about Tasker.

Ps: I have no knowledge about IOS, Windows , or Mac platform pertaining to this automation issue. So go figure it out yourselves, in case you are using one of these platforms, leave a comment below explaining how you did it. And I will create a new post for the same.


Setup


You need to have the at utility which requires a download of a few kb's, if  not preinstalled. I am using Ubuntu 14.04 LTS , and this utility isn't pre-installed. So you need to fire the command

sudo apt-get install at 

The Internet disable & enable commands

  • Disable: nmcli nm enable true
  • Enable : nmcli nm enable false 

Automation

Now that we have got the commands to enable/ disable the Internet, we are now set to go.

  •  Setup all your downloads, torrents etc. Make sure that you use a download manger which supports resumable downloads. Or alternatively just note down the download link and use a shell script to download it using curl. 
  • Disable the Internet using the above command "nmcli nm enable false". To be fired in the terminal without the double quotes. In case, this doesn't work out use sudo and enter the root password.
  • Now that we have disabled the Internet , everything will be waiting for wifi or ethernet connectivity. Now we need to fire the following commands:

 echo "echo 'rootpasswordhere' | sudo -S nmcli nm enable true"|at 02:02
 echo "echo 'rootpasswordhere' | sudo -S nmcli nm enable false"|at 04:58
  •  Just note down the job number in case you ever need to  cancel the job. eg "atrm 1" will cancel the job with job id 1.

Additional Notes

The aim of the article was to demonstrate the use of at command. Now that it is covered here are a few ideas. 

  • Instead of shutting down Internet you could just launch and close your torrent client.This would mean no loss of Internet connectivity.However any other application might still get Internet access. Assuming its transmission here is your command

 echo "echo 'rootpasswordhere' | sudo -S pkill transmission"|at 04:58

  •  If this was done solely for the purpose of downloading then I suggest saving some power and improving your karma and reducing your power bill of course.
        echo "echo 'rootpasswordhere' | sudo -S shutdown -h now"|at 04:59

  • Or alternatively you could go with 
echo "echo 'rootpasswordhere' | sudo -S shutdown -P now"|at 04:59
  • Or
echo "echo 'rootpasswordhere' | sudo -S shutdown -P 20"|at 04:59

  • I suggest you test the shutdown commands, see which works the best and shuts down your pc/laptop correctly. 
  • In case, this is a frequent but not permanent recurring job I suggest making a script at running it before you go to doze off.
  • Lastly, in case this is a permanent recurring job. Make a script and run this script using crontab utility.
  • You can also selectively disable what kind of internet access you wish to disable, is it Ethernet or wifi etc.







Image Credits:Pixabay









No comments:

Post a Comment