Building Professional Web Hosting Solution
<< Securing and Protecting Linux System Course
>> Linux Hardening Rules and IPtables Firewall Section

Preparing Linux Script Startup Environment is a must step for various scripts I am going to build such Kernel rules, IPtables Modules, Optimization Rules and any other scripts that I might need to run at the startup. I have actually dedicated an article for that since it varies little bit between Debian and Red Hat Based system, and between the Oldest and New method when using Systemd. However, the concept still the same is to have a script get executed at the startup.
Objectives:
1. Creating Centralized Startup Environment File
2. Debian Based: Enable Startup Script Older Releases
3. Red Hat Based: Enable Startup Script Older Releases
4. Systemd Based: Enable Startup Script Newer Releases
5. Listing and Verifying Custom Scripts Startup Environment
Prerequisites:
A. Basic Debian and Red Hat System Knowledge
B. Login to your DigitalOcean or Vultr Account
C. Linux Basic Setup and Configuration Course
Recommendations:
1. For better performance, use VPS with at least 2 CPUs, 4G Memory, 1G Bandwidth, and SSD Storage drive.
2. All public VPS Nodes must equipped with Web command line Interface, hence, login to the web interface and have it ready in case you need to stop the Firewall. Everything was tested and it should not lock you out or terminate the SSH session, but just to be on the safe side, have the web command line interface ready.
Table of Contents
Creating Centralized Startup Environment File
The idea is to create a centralized startup environment file which would be Executable file “custom-scripts.sh” to load all executable scripts. First, create the file, then enable it at the startup.
Run the following steps (1 to 3)
1. First, make sure nano editor is there
a. Debian Based
apt-get update && apt-get install nano -y
b. Red Hat Based
yum install nano -y
2. Create custom-scripts.sh Startup File
nano /etc/init.d/custom-scripts.sh
Copy and paste the below info into your custom-scripts.sh
#!/bin/bash ### BEGIN INIT INFO # Provides: custom-scripts # Required-Start: $local_fs $network # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: custom-scripts # Description: Applying Customized Startup Scripts ### END INIT INFO echo echo "Applying Customized Startup Scripts..." echo ################################################# # Write down your script's path below
Save: Ctrl-X, Hit Y Key, and Enter
3. Apply Execution Permission
chmod +x /etc/init.d/custom-scripts.sh
4. Allow Only Root Access to the Script
chmod 700 /etc/init.d/custom-scripts.sh
Debian Based: Enable Startup Script Older Releases
Note: Only for Debian7, Ubuntu14.04 and older systems. For Debian8, Ubuntu16.04 or above, scroll down to use Systemd Based startup environment instead as shown below.
Add custom-script.sh to Startup
update-rc.d custom-scripts.sh defaults
- defaults: run levels 2 through 5
To remove the Custom Script from Startup:
update-rc.d -f custom-scripts.sh remove
Red Hat Based: Enable Startup Script Older Releases
Note: Only CentOS6 system and older systems. For CentOS7 or above, scroll down to use Systemd Based startup environment instead as shown below.
Run the following steps (1 to 3)
TIP: Go with Method 1 and leave Method 2 as Backup in case Method 1 did not work.
Method 1:
1. Add custom-scripts.sh to Run Level 3, 4, and 5
chkconfig --level 345 custom-scripts.sh on
2. Enable custom-scripts.sh at Startup
chkconfig custom-scripts.sh on
Disable the Script from Running at the Startup:
chkconfig custom-scripts.sh off
Method 2:
Is Old School…
1. Make sure rc.local executable
chmod +x /etc/rc.d/rc.local
2. Edit Startup Environment File
nano /etc/rc.d/rc.local
3. Enable the Script
Copy and paste the custom-scriptes.sh path at the end of rc.local file as shown.
# Custom Startup Scripts File /etc/init.d/custom-scripts.sh
Save: Ctrl-X, Hit Y Key, and Enter
To disable the Script, append it using hash sign.
# Custom Startup Scripts File #/etc/init.d/custom-scripts.sh
Systemd Based: Enable Startup Script Newer Releases
Note: Only Debian8, Ubuntu16.04, CentOS7 and newer systems.
By default /etc/rc.local and /etc/rc.d/rc.local are no longer executable in systemd. Systemd does not by default execute /etc/rc.local, which was used to store custom scripts or commands at startup in pre-Systemd systems.
Using systemd Based Systems, create and enable custom-scripts.service using systemd service file.
Run the following steps (1 to 5)
1. Create the custom-scripts.service file
nano /etc/systemd/system/custom-scripts.service
Copy and paste the below info into your custom-scripts.service file.
[Unit]
Description=/etc/custom-scripts.sh compatibility
[Service]
Type=oneshot
ExecStart=/etc/init.d/custom-scripts.sh
# disable timeout logic
TimeoutSec=0
#StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
Save: Ctrl-X, Hit Y Key, and Enter
2. Enable the custom-scripts.service file
systemctl enable custom-scripts.service
3. Reload Systemd
systemctl --system daemon-reload
4. Start the custom-scripts.service file
systemctl start custom-scripts.service
5. Check the Status of custom-scripts.service file
systemctl status custom-scripts.service
You should see Active Green color:
Note: if you see Red Color messages, then you forget to set the startup script as executable.
● custom-scripts.service - /etc/custom-scripts.sh compatibility Loaded: loaded (/etc/systemd/system/custom-scripts.service; enabled) Active: active (exited) since Thu 2017-02-23 12:51:41 EST; 2s ago Process: 767 ExecStart=/etc/init.d/custom-scripts.sh (code=exited, status=0/SUCCESS) Main PID: 767 (code=exited, status=0/SUCCESS)
To disable custom-scripts.sh Startup
systemctl stop custom-scripts.service
systemctl disable custom-scripts.service
systemctl --system daemon-reload
Listing and Verifying Custom Scripts Startup Environment
a. Debian Based List Services Older Releases
For Only Debian7, Ubuntu12.04, Ubuntu14.04 and older systems. Note: for Debian8, Ubuntu16.04 or above, use Systemd Based Verification steps instead as shown below.
List and Verify custom-scripts.sh File
ls -lah /etc/rc* | grep custom-scripts
You should see the output similar to the following, which indicates the custom-scripts.sh file is ready to load custom scripts.
lrwxrwxrwx 1 root root 27 May 27 18:36 K01custom-scripts.sh -> ../init.d/custom-scripts.sh lrwxrwxrwx 1 root root 27 May 27 18:36 K01custom-scripts.sh -> ../init.d/custom-scripts.sh lrwxrwxrwx 1 root root 27 May 27 18:36 S01custom-scripts.sh -> ../init.d/custom-scripts.sh lrwxrwxrwx 1 root root 27 May 27 18:36 S01custom-scripts.sh -> ../init.d/custom-scripts.sh lrwxrwxrwx 1 root root 27 May 27 18:36 S01custom-scripts.sh -> ../init.d/custom-scripts.sh lrwxrwxrwx 1 root root 27 May 27 18:36 S01custom-scripts.sh -> ../init.d/custom-scripts.sh lrwxrwxrwx 1 root root 27 May 27 18:36 K01custom-scripts.sh -> ../init.d/custom-scripts.sh
b. Red Hat Based List Services Older Releases
For Only CentOS6 system and older.
chkconfig --list | grep custom-scripts
You should see the output similar to the following, which indicates the custom-scripts.sh file is ready to load custom scripts.
custom-scripts.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off
c. Systemd Based List Services Newer Releases
For Only Debian8, Ubuntu16.04, CentOS7 and newer systems using Systemd.
systemctl list-unit-files | grep custom-scripts
The script is Enabled.
custom-scripts.service enabled
Summary
I will be loading few scripts at the startup during advanced configuration, this actually will be a test to the custom-scripts.sh functionality. Besides, did you notice that I mentioned systemd base systems? Systemd will eventually allow Linux flavors to have industry Standardized command interface across all the different type of Linux distribution, especially when it comes to Cli management.
Subject Related
By How do Services Work? | Configure a Service to Startup Automatically | Manage Debian Startup Scripts | CentOS Init Startup
Building Professional Web Hosting Solution
<< Securing and Protecting Linux System Course
>> Linux Hardening Rules and IPtables Firewall Section
LEAVE A COMMENT