Tuesday, December 4, 2007

Unix at command - very nice !

My Coworker sent our croup an email about a very useful command and I wanted to share it with the rest of the world.
he Unix "at" command is very useful for taking care of things that you want to run only once, but at a specific time. Its essentially just way to schedule a command (or several commands) to execute at any given time without you actually having to interactively log into a shell and run the command(s) at said time.

So supposed suppose you have this script that sqlplus as sysdba and then shutsdown your database etc...(maybe even your application tier i.e. 11i )
Script: $ORACLE_HOME/dba/scripts/shutdown.sh
So u have to take a db down at a certain time but being the dba that you are, u dont wanna have to remember, Hence th at command :).
also suppose your datase is the only db in a box called Fado.

this is how you do it:


---------------------------------------------------
[ root@fado- Wednesday, November 28, 2007 - 10:19:27 AM]
[ /home/fayoubi] at 1100
at> $ORACLE_HOME/dba/scripts/shutdown.sh stop > /tmp/fahd.txt
at> mailx -s "Fadois down." -r fayoubi@gmail.com fayoubi@gmail.com < /tmp/fahd.txt at> rm /tmp/fahd.txt
at>
commands will be executed using /bin/sh
job 1196269200.a at Wed Nov 28 11:00:00 2007
---------------------------------------------------


So in this example, I entered "at 1100" (time is in 24hour format)... this dumped me into an at> prompt. Here I entered the command(s) that I wanted to run. Once you've entered all of the commands that you want to happen, press CTRL+D to signal that you're done and it will give you a confirmation that the commands will be executed at the requested time and dump you back out into your shell. In this example, I used 3 commands to execute shutdown.sh and email me the output. First command executes shutdown.sh stop and redirects that output into /tmp/fahd.txt... second command emails me the contents of /tmp/fahd.txt (which will be the shutdown.sh output)... and finally the third command removes /tmp/fahd.txt . The "" is where I pressed CTRL+D to exit the at> prompt. The email that I received from this job can be seen at the end of this note.

Quick note: if it s allready 15:00 and you type at 13:00, the command will execute the next day !
Hope you found this very useful.
Again, thanks for reading

No comments: