Keywords: Linux | Shell Scripts | Automatic Startup | init.d | System Services
Abstract: This comprehensive technical article explores multiple methods for automatically executing shell scripts during Linux system boot, with detailed focus on init.d service configuration including script permissions, symbolic linking, and LSB compliance requirements. The guide compares crontab @reboot and rc.local approaches, provides practical implementation examples, and extends to desktop environment autostart configurations, offering complete solutions for various deployment scenarios.
Fundamentals of Linux Startup Scripts
The Linux boot process follows a specific initialization sequence involving multiple runlevel transitions. Traditional System V init systems utilize the /etc/init.d directory for service management, where scripts are activated through symbolic links to corresponding rc.d directories. Understanding this mechanism is essential for proper configuration of automatic startup scripts.
Detailed init.d Service Script Configuration
Creating init.d service scripts represents the professional approach to automatic startup. Begin by creating an executable script file in /etc/init.d:
sudo touch /etc/init.d/start_my_app
sudo chmod +x /etc/init.d/start_my_app
The script content must adhere to LSB (Linux Standard Base) specifications, providing comprehensive service management capabilities. Below is a standardized init.d script template:
#!/bin/sh
### BEGIN INIT INFO
# Provides: start_my_app
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start my Node.js application
# Description: Starts the Node.js application using forever
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/forever
SCRIPT=/var/myscripts/start_my_app
NAME=start_my_app
DESC="My Node.js Application"
case "$1" in
start)
echo "Starting $DESC: $NAME"
$DAEMON start $SCRIPT
;;
stop)
echo "Stopping $DESC: $NAME"
$DAEMON stop $SCRIPT
;;
restart)
echo "Restarting $DESC: $NAME"
$DAEMON restart $SCRIPT
;;
status)
$DAEMON list
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Symbolic Linking and Runlevel Configuration
After creating the init.d script, establish symbolic links to rc.d directories to enable the service:
sudo ln -s /etc/init.d/start_my_app /etc/rc2.d/S99start_my_app
sudo ln -s /etc/init.d/start_my_app /etc/rc0.d/K01start_my_app
Here S99 indicates startup priority (lower numbers have higher priority), while K01 represents shutdown priority. This configuration ensures automatic service execution during system boot and proper termination during shutdown.
Crontab @reboot Method
For simpler startup requirements, utilize crontab's @reboot feature. First create a user-level startup script:
echo '#!/bin/bash
/var/myscripts/start_my_app' > /home/user/startup.sh
chmod +x /home/user/startup.sh
Then configure crontab:
crontab -e
# Add the following line
@reboot /home/user/startup.sh
This approach suits user-level startup tasks but lacks comprehensive service management capabilities.
/etc/rc.local Configuration Approach
For rapid deployment, edit the /etc/rc.local file:
#!/bin/sh -e
# Commands to execute during system boot
/var/myscripts/start_my_app &
exit 0
Note the use of & operator to run the process in background, preventing boot process blocking. This method offers simplicity but lacks service monitoring and management features.
Desktop Environment Autostart Configuration
In graphical desktop environments, automatic startup upon user login can be achieved by creating .desktop files:
mkdir -p ~/.config/autostart
cat > ~/.config/autostart/myscript.desktop << EOF
[Desktop Entry]
Type=Application
Name=My Startup Script
Exec=/var/myscripts/start_my_app
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
EOF
This method is suitable for applications requiring graphical interface environments.
Best Practices and Troubleshooting
When configuring startup scripts, always use absolute paths for all file and command references to prevent execution failures due to environment variable issues. Ensure scripts have proper execution permissions and verify compatibility in target environments through testing. For production environments, the init.d approach is recommended for complete service management capabilities including start, stop, restart, and status query functionalities.
For debugging startup scripts, examine system logs for detailed information:
tail -f /var/log/syslog
dmesg | grep -i startup
systemctl status start_my_app
These tools enable rapid identification and resolution of issues encountered during the boot process.