[Linux] How to Auto-Execute Commands or Programs at Startup

Tadashi Shigeoka ·  Sat, September 24, 2011

I looked up how to auto-execute commands or programs when CentOS starts up, so here’s a memo.

When you want users to execute custom programs at OS startup, write the startup commands in the /etc/rc.d/rc.local file.

The /etc/rc.d/rc.local file is a shell script executed at the end of the startup process.

I wanted to set up authenticated proxy with export and time synchronization with ntpdate at startup, so I did the following:

■ /etc/rc.d/rc.local (before editing)

#!/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

■ /etc/rc.d/rc.local (after editing)

#!/bin/sh

touch /var/lock/subsys/local

# Proxy authentication
export http_proxy=http://[username]:[password]@[IP address]:[port number]/
# Time synchronization
ntpdate [IP address]

That’s all from the Gemba.