Skip to main content

Run a Shell Script as Systemd Service in Linux

Run a Shell Script as Systemd Service in Linux 


In this post,I will make a manual script and register it as a SystemD Service in Linux.

Systemd is a software application that provides an array of system components for Linux operating systems. It is the first service to initialize the boot sequence. This always runs with pid 1. This also helps use to manage system and application service on our Linux operating system.

We can also run any custom script as systemd service. It helps the script to start on system boot. This can be helpful for you to run any script which required to run at boot time only or to run always


 
1) Create a script to monitor file system space

$ sudo vi /usr/bin/filesystem_check.sh
Then, Copy and paste the following script and save your file:

#!/bin/bash
# Script check filesystem utilization every 120 seconds store in a file
while true
do
date >> /var/log/fs-monitor.txt
sudo df -h  >> /var/log/fs-monitor.txt
sleep 120
done

 
2) Make the file executable by running the following command.

$ chmod +x /usr/bin/filesystem_check.sh

3) Make a service for running the script. 
Just create a file in the following directory. Note you can give any name but it must end with .service extension:

$ vi /etc/systemd/system/fs-monitor.service
And add the following details:

[Unit]
Description=FS monitoring service
Documentation=https://funoracleapps.com/

[Service]
Type=simple
User=root
Group=root
TimeoutStartSec=0
Restart=on-failure
RestartSec=30s
#ExecStartPre=
ExecStart=/usr/bin/filesystem_check.sh
SyslogIdentifier=Diskutilization
#ExecStop=

[Install]
WantedBy=multi-user.target


Explanation

The [Unit] section consists of description, documentation details etc

[Service] Section defines the service type, username, group, what to do in failure, restart timeout. The main is 'ExecStart' which says to start our script file. You can also define 'ExecStartPre' to define anything before the actual script file.'SyslogIdentifier' is the keyword to identify our service in syslog. Similarly, ExecStop is the instruction to say what to do to stop the service.

[Install] section is used to define different levels of target in the system.


4) Save the file and start the service using the systemctl command:

$ systemctl start fs-monitor.service
$ systemctl status fs-monitor.service




5) Verify that your script and check log file:

$ cat /var/log/fs-monitor.txt

6)Enable or disable to start at system boot

$ systemctl enable fs-monitor.service



$ systemctl disable fs-monitor.service

 
7) Stopping and Restarting Services

$ systemctl stop fs-monitor.service
$ systemctl restart fs-monitor.service





If you like please follow and comment

Comments

Popular posts from this blog

WebLogic migration to OCI using WDT tool

WebLogic migration to OCI using WDT tool Oracle WebLogic Deploy Tool (WDT) is an open-source project designed to simplify and streamline the management of Oracle WebLogic Server domains. With WDT, you can export configuration and application files from one WebLogic Server domain and import them into another, making it a highly effective tool for tasks like migrating on-premises WebLogic configurations to Oracle Cloud. This blog outlines a detailed step-by-step process for using WDT to migrate WebLogic resources and configurations. Supported WLS versions Why Use WDT for Migration? When moving Oracle WebLogic resources from an on-premises environment to Oracle Cloud (or another WebLogic Server), WDT provides an efficient and reliable approach to: Discover and export domain configurations and application binaries. Create reusable models and archives for deployment in a target domain. Key Pre-Requisites Source System: An Oracle WebLogic Server with pre-configured resources such as: Applica...

How to Validate TDE Wallet Password in Oracle Database

How to Validate TDE Wallet Password in Oracle Database Validating the Transparent Data Encryption (TDE) wallet password is crucial, especially when ensuring that the password is correct without using the OPEN or CLOSE commands in the database. This blog post explains a straightforward method to validate the TDE password using the mkstore utility. Steps to Validate TDE Wallet Password Follow these steps to validate the TDE wallet password: Step 1: Copy the Keystore/Wallet File Navigate to your existing TDE wallet directory. Copy only the ewallet.p12 file to a new directory. If a cwallet.sso file exists, do not copy it . The absence of cwallet.sso ensures that the wallet does not use auto-login, forcing the utility to prompt for the password. Step 2: Validate Using mkstore Use the mkstore utility to check the contents of the wallet file. The mkstore utility will prompt you for the TDE wallet password, allowing you to validate its correctness. Command Syntax To display the conten...

Rename a PDB in Oracle Database Multitenant Architecture in TDE and Non TDE Environment

Rename a PDB in Oracle Database Multitenant Architecture I am sharing a step-by-step guide to help you rename a PDB. This approach uses SQL commands. Without TDE or encryption Wallet Initial Check Check the Current Database Name and Open Mode: SQL > SELECT NAME, OPEN_MODE FROM V$DATABASE; NAME OPEN_MODE --------- -------------------- BEECDB READ WRITE List Current PDBs: SQL > SHOW PDBS; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 FUAT READ WRITE NO We need to RENAME FUAT to BEE  Steps to Rename the PDB Step 1: Export ORACLE_SID Set the Oracle SID to the Container Database (CDB): export ORACLE_SID=BEECDB Step 2: Verify Target PDB Name Availability If the target PDB name is different from the current PDB name, ensure no service exists with the target PDB name. Run SQL to Check Exi...