• 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. Beginners guide to vi editor (command line reference)
  2. How to Disable/Enable Numa for Virtual Machine (XEN based)
  3. CentOS / RHEL 7 : unable to start vsftpd service
  4. “Leap status Not synchronised” – On running ‘chronyc tracking’
  5. Oracle Database Environment Variables and Their Functions
  6. CentOS / RHEL 6 : How to setup yum repository using locally mounted DVD
  7. What is the difference between & (ampersand) and && (double ampersand) while executing simultaneous commands on Linux
  8. How to mount and umount a file system in Linux
  9. “Couldn’t find device with uuid [UUID]” – error whith pvs command
  10. ‘nestat -s’ showing a large number for “packet reassembles failed” errors in CentOS/RHEL

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright