- Forums
- Linux
- How To Execute Command During Booting
today we are going to show you a little cool administration trick lets say for example you want to execute a shell command when your server starts to boot [3299], Last Updated: Mon Jun 24, 2024
Webune Support
Sat Feb 27, 2010
0 Comments
411 Visits
hello, welcome to Webune Support Forums
Webune provides Linux Dedicated servers!!!
Today we are going to show you a little cool administration trick.
lets say for example you want to execute a shell command when your server starts to boot.
lets say for the purpose of this tutorial, we want to create a file called mylog.txt when the computer boots up.
#########:: step one is to login as root or login as superuser ::** #######
normally, if you are logged into your shell and if you send this command it will create a file called mylog.txt in the /root/ directory
SHELL COMMAND:
date '+LAST BOOT:%nDATE: %m/%d/%y%nTIME:%H:%M:%S' > /root/test.txt
if you send this command it will create a file called mylog.txt and it will append with the following message:
LAST BOOT:
DATE: 02/27/10
TIME:18:14:57ok, lets say i want to log the last time the server booted and write the time and date when this happen.
Some Linux distributions include a startup script that runs after the other SysV startup scripts. This script is generally called
/etc/rc.d/rc.local, /etc/rc.d/boot.ocal or something similar. - NOTE that i am using a CentOS server, so if you have a RedHat or Fedora, it should be the file called
/etc/rc.d/rc.localWARNING: as humans we always make mistakes, so to cover ourselves, lets make a backup copy of the original
/etc/rc.d/rc.local file with this command;
SHELL COMMAND:
cp /etc/rc.d/rc.local /etc/rc.d/rc.local-ORIGINAL
since i am using CentOS, CentOS comes with nano as a text editor. you can use your favorite editor like VI if you want to.. so send this command to edit the
/etc/rc.d/rc.local file:
SHELL COMMAND:
nano /etc/rc.d/rc.local
you might see something like this:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
so add our command at the last line after touch /var/lock/subsys/local, so we add this:
date '+LAST BOOT:%nDATE: %m/%d/%y%nTIME:%H:%M:%S' > /root/test.txtsave the changes, now reboot your server
after the server has rebooted, view the contents of /root/test.txt with the cat command:
cat /root/test.txt
COOL!! now you should see the date has changed to when the server was last booted