
Automating Bash scripts on Linux is essential for system administration tasks, such as installing applications and monitoring system logs. Cron is a job scheduler that runs tasks at specific intervals, while systemd serves as a service manager, handling applications through service profiles in Linux.
This article explains how to automate Bash scripts on Linux using Cron and Systemd to execute and manage system tasks automatically.
Before you begin:
Bash scripts support multiple commands and functions that define how tasks run in a system. Follow the steps below to create new Bash scripts to back up critical web application files and monitor the system logs for errors.
Switch to your user's home directory.
Create a new log-monitor.sh script.
Add the following contents to the log-monitor.sh file.
Save and close the file.
The above Bash script creates error_log.txt and warning_log.txt files in the /opt/data directory to store filtered result from the /var/log/syslog system logs file using a while loop. The script filters the result and writes it to the respective error or warning file every time a new error displays in the /var/log/syslog file.
Create another backup.sh script.
Add the following contents to the file.
Save and close the file.
The above Bash script connects to the MySQL database server using the DB_... variables. The script then creates mysql_backup and web_backup directories to store the database and web applications' backups.
Move the Bash script to a system-wide directory like /opt.
Create a new data directory under the system-wide directory you've chosen.
Change the directory permissions to 777 to allow all users to write files.
Cron creates jobs that you can use to schedule and automatically run a Bash script at specific intervals. Follow the steps below to automate the Bash scripts you created earlier to automatically back up the MySQL database and web application files daily.
Open the Crontab editor.
Select your desired text editor to edit the Crontab file when prompted. For example, select 1 to choose the nano text editor.
Add the following directive at the end of the file.
Save and close the file.
The above cron job runs the backup.sh script daily at 2:00 A.M. to back up the database and web application files. In the above cron job:
0: Runs the script at the start of a minute. Set a value between 0 and 59.2: Runs the Bash script at 2:00 A.M. Specify an hour between 0 and 23.*: Runs the Bash script every day of the month. Specify a day between 1 and 31.*: Runs the Bash script every month. Specify a month between 1 and 12.*: Runs the bash script daily. Specify a day of the week between 0 and 7.List all Cron jobs and verify that the new Bash script task is available.
Output:
Systemd starts system services automatically at boot and offers options for stopping and restarting the services. Follow the steps below to set up the monitoring Bash script you created earlier as a new system service and manage the service using systemd.
Create a new system service file, such as logmonitor.service.
Add the following contents to the logmonitor.service file.
Save and close the file.
The above service configuration runs the log-monitor.sh bash script in the /opt directory to monitor and filter log entries in the /var/log/syslog file.
Reload systemd to apply the service file changes.
Enable the new system service to automatically start at boot.
Output:
Start the system service.
View the system service status and verify it's running.
Output:
Run the following commands to simulate new error and warning entries in the /var/log/syslog file.
List all files in the /opt/data directory and verify that the error_log.txt and warning_log.txt files are available.
Output:
View the error_log.txt file and verify that only the error entries are available in the file.
Output:
You have automated Bash scripts on a Linux server using Cron and Systemd. Automating Bash scripts allows you to run common system administration tasks, such as backups and error monitoring. To streamline system management, create multiple Bash scripts and Cron jobs, specifying the necessary commands for each.
0 Comments
Be the first to comment and share your perspective with the community.