Tuesday, August 7, 2007

Job Scheduling

I love Linux Shell Scripts. That is one of the coolest areas to work with. Traditionally in Windows we used to work with MS DOS commands. But Linux shell scripting interests me the most.

Cron is the infamous buzzword for scheduling jobs. It helps you schedule jobs chronologically. There is another command that helps schedule one-time jobs. Think about it, your machine is used as a server for a lot of people working, connecting to your machine. You will not be there till every one of them finish using your server. Once they finish, your server well needs to be shutdown for maintenance. But as people are selfish, you cant trust them to do the shutdown for you. But well, yes, you can very well trust your program to do that for you.

atd is the the Linux command used to schedule one-time jobs queued for later execution. atd is the service which has to be enabled and running to do this.

Some Examples:




[samjosh@Samjosh ~]$ /sbin/service atd restart





# Schedule system shutdown at 18:20
[samjosh@Samjosh ~]$ at 20:30
at> poweroff
at>
job 16 at 2007-08-16 20:30
[samjosh@Samjosh ~]$





# To see all the jobs in queue
[samjosh@Samjosh ~]$ atq
16 2007-08-16 20:30 a samuelt
[samjosh@Samjosh ~]$

Well then what if all of the clients shutdown before the scheduled time? You need to check constantly whether any of your client is alive. If so, wait... otherwise its safe to power off your machine.

Here are the snippets!




# Checks status of your client
host=192.168.0.118
ping -c 1 $host
if [ `echo $?` -ne 0 ]
then
echo $host" is alive"
else
echo $host" is not alive"
fi

# Tip
Use as in the above snippet for each of your clients. Do this in a while loop. If a client is alive you can continue the loop as follows:




while [ 1 ]
do

{if - host1 is alive }
shift
continue
{else - check for host2 }
....
{if - host n is dead }

echo "System shutdown at `date` " >> ~/Desktop/my_log.txt
poweroff

done


Cool! I luvvit... Samjosh.

1 comment:

Anonymous said...

Thanks, I'm not familiar with Linux shell scripting, but this seems pretty important to know.