• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

CentOS / RHEL 7 : How to create custom script to run automatically during boot

by admin

In RHEL 5 and 6, we were using automatic startup feature of RHEL through /etc/rc.d/init.d to run any script at system boot. Starting with RHEL 7 init is replaced by systemd and the prior method is now deprecated. There is another way in RHEL 7 to do the same.

Creating the custom script

1. Let us first create a sample custom script to be run at system boot automatically.

# vi /var/tmp/test_script.sh
#!/bin/bash
echo "This is a sample script to test auto run during boot" > /var/tmp/script.out
echo "The time the script run was -->  `date`" >> /var/tmp/script.out

2. Check and verify the file permission.

# ls -lrt /usr/local/sbin/myscript.sh

3. Add execute permission(if it’s not already set).

# chmod +x /var/tmp/test_script.sh

Creating new systemd service unit

Create a new service unit file at /etc/systemd/system/sample.service with below content. The name of the service unit is user defined and can be any name of your choice.

# vi /etc/systemd/system/sample.service
[Unit]
Description=Description for sample script goes here
After=network.target

[Service]
Type=simple
ExecStart=/var/tmp/test_script.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

Here,

After= : If the script needs any other system facilities (networking, etc), modify the [Unit] section to include appropriate After=, Wants=, or Requires= directives.
Type= : Switch Type=simple for Type=idle in the [Service] section to delay execution of the script until all other jobs are dispatched
WantedBy= : target to run the sample script in

Enable the systemd service unit

1. Reload the systemd process to consider newly created sample.service OR every time when sample.service gets modified.

# systemctl daemon-reload

2. Enable this service to start after reboot automatically.

# systemctl enable sample.service

3. Start the service.

# systemctl start sample.service

4. Reboot the host to verify whether the scripts are starting as expected during system boot.

# systemctl reboot

Filed Under: CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. How to Install Oracle Linux (UEK-2) with btrfs as a root filesystem
  2. nslookup: command not found
  3. Windows Active Directory Account Shows Inconsistent UID/GID In Different Linux SSSD Clients (CentOS/RHEL)
  4. dir Command Examples in Linux
  5. RHEL 7 – RHCSA Notes : Change passwords and adjust password aging for local user accounts
  6. daemon: command not found
  7. How to identify the HBA cards/ports and WWN in Linux
  8. How to Change Kernel Semaphore Limits in CentOS/RHEL
  9. How to use shell expansions for generating shell tokens under Linux
  10. How to Convert a Directory to Partition in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • ncat Command Examples in Linux
  • ncat: command not found
  • nautilus Command Examples in Linux
  • namei: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright