Thursday, November 26, 2009

Schedule an one time job in linux - the "at" command & "sudo" time out option

Wanted to download and build from source the new Chromium OS, when I came across a huge list of packages as pre-requisites for Chromium OS. The download over the internet being free after 2:00 AM in the night, it had to be scheduled around that time.

There is an "autodownload.sh" script which will download & install the files (detailed at the end of this post). This script had to be scheduled for execution at 2:10 AM.

To schedule the job
benoy@palazhi:~$ sudo at -f autodownload.sh -v 2:10 AM
[sudo] password for benoy: [enter the password]
Fri Nov 27 02:10:00 2009

warning: commands will be executed using /bin/sh
job 15 at Fri Nov 27 02:10:00 2009

[NOTE]
1. The "at" command is preceeded by the "sudo" command since the script "autodownload.sh" requires root privileges to execute and install the packages.
2. The file name (name of the script file which is to be executed at a scheduled time) is given with "-f" option.
3. The time at which the script has to be executed is given with "-v" option.
4. The output shows that a job, with ID 15, has been scheduled for execution at 02:10:00 hours on Nov 27, 2009.
5. For scheduling periodic jobs, you can create a "crontab" using a "crontab" command with "-e" option.
[/NOTE]

To see the scheduled jobs
benoy@palazhi:~$ sudo atq
15 Fri Nov 27 02:10:00 2009 a root

[NOTE]
1. The "atq" command lists the scheduled jobs.
2. It is preceeded by the "sudo" here as the earlier job was added as job to be executed by the root.
[/NOTE]

To delete the scheduled job
benoy@palazhi:~$ sudo atrm 15
benoy@palazhi:~$ sudo atq
benoy@palazhi:~$

[NOTE]
1. The "atrm" command cancels the job with the ID mentioned in the command line (15 in this case).
2. The "atrm" command is preceeded by the "sudo" command as the earlier job was added as job to be executed by the root.
[/NOTE]

Now lets see the autodownload.sh script

benoy@palazhi:~$ cat autodownload.sh
#!/bin/sh
rm -f temp
echo "Deleted temp.log..." >> temp.log
echo "Start downloading..." >> temp.log

while read package
do
sudo apt-get install -y --install-recommends $package >> temp.log 2>>temp.log
done < packages.txt

echo "Halting the system....." >> temp.log
halt -p

[NOTE]
1. The while loop reads from the file "packages.txt", line by line, to a user defined variable "package".
2. The "sudo apt-get install..." line has "$package" to get the name of the package (which is read from the file packages.txt).
3. The "apt-get" command is preceeded by a "sudo" command as it involves package installation which requires root privilege.
4. The "-y" option to "apt-get" provides an automatic "yes" to the "apt-get" prompts.
5. The "--install-recommends" options ensures that recommended packages are also installed.
6. The errors are as well appended to "temp.log" file (note "2>>temp.log" at the end of the "sudo apt-get..." command.
7. The "halt -p" command shuts down the system after completing the download & installations.
[/NOTE]

We now have another issue at hand. The sudo command will time out in 5 or 15 minutes, so at 2:10 AM, when the "sudo apt-get..." command will be executed, the system will require the root password again (which will defy the very purpose of scheduling this download). The way around is to give a high or an indefinite value for time out.

To change the sudo timeout
benoy@palazhi:~$ sudo visudo

The system opens the "/etc/sudoers" file; add the following line:
Defaults:benoy timestamp_timeout=-1

[NOTE]
1. Change "benoy" to your user name.
2. A negative value for timestamp_timeout means indefinite timeout, i.e till you logout or exit from the terminal.
3. A positive value will ensure that the sudo session remains active for that many minutes.
4. Do a "sudo -K" to forcefully end a sudo indefinite session.
[/NOTE]

Now, have a file "packages.txt"; which will list the packages that are to be downloaded. Let us keep it very simple; each line of the file will have the name of one package that is to be dowloaded.

benoy@palazhi:~$ cat packages.txt
subversion
pkg-config
python
perl
g++
g++-multilib
bison
flex
gperf
libnss3-dev
libgtk2.0-dev
libnspr4-0d
libasound2-dev
libnspr4-dev
msttcorefonts
libgconf2-dev
libcairo2-dev libdbus-1-dev

No comments:

Post a Comment

Followers