Back to Blog
Lesson 24 of the Linux: Linux Command Line for Developers course
LinuxAugust 11, 20263 min read

Project Task: Automating Service Scripts with Cron

Learn how to automate system maintenance by building a shell script to restart your web server, scheduling it with cron, and verifying your execution logs.

linuxbashautomationcronshell scriptingdevops
A cluttered desk with a tablet showing a to-do list, monitor showing code, and office supplies.

Previously in this course, we covered the fundamentals of Introduction to Cron Jobs: A Guide to Linux Task Automation, where you learned the syntax for scheduling recurring tasks. In this lesson, we are moving from simple one-line commands to writing a production-ready shell script for our web server project, automating its maintenance, and ensuring we can track its output.

From Manual Commands to Automation

As a developer, you rarely want to manually restart a service when it hangs or needs a refresh. By encapsulating these commands into a shell script, you ensure consistency, reduce human error, and create a reusable piece of infrastructure.

For our web server project, we need a script that performs a "health check" and restarts the service if necessary.

Step 1: Writing the Service Script

We will create a script that checks for the existence of our web server process and restarts it if it's missing. Using what you learned in Introduction to Processes, we know we can use pgrep to check for running services.

Create a file named restart_server.sh in your project directory:

Bash
#!/bin/bash

# Define our log file path
LOGFILE="/var/log/web_server_restart.log"

# Check if the web server process is running
if pgrep -x "nginx" > /dev/null
then
    echo "$(date): Web server is running." >> $LOGFILE
else
    echo "$(date): Web server stopped. Restarting..." >> $LOGFILE
    systemctl restart nginx
fi

Key takeaways from this code:

  • Shebang (#!/bin/bash): Tells the system which interpreter to use.
  • Redirection (>>): We use this to append logs; refer back to Standard Streams and Redirection if you need a refresher on how this works.
  • Permissions: Before this script can run, you must make it executable using chmod +x restart_server.sh, a topic we explored in Modifying Permissions with Chmod.

Step 2: Scheduling with Cron

Now that the script is ready, we need to schedule it. We'll use the crontab -e command to add a task that runs this script every hour.

Add this line to your crontab:

Bash
0 * * * * /home/user/project/restart_server.sh

Step 3: Verifying Execution Logs

Automation is useless if you don't know whether it succeeded. Because our script redirects output to a log file, we can verify its execution history. Use the tail command to monitor the log file we defined:

Bash
tail -f /var/log/web_server_restart.log

If the script runs correctly, you will see timestamps appearing in the log. If you see "Permission denied," ensure your user has write access to that log file location.

Common Pitfalls

  1. Relative Paths in Cron: Cron runs from the user's home directory by default. Always use absolute paths (e.g., /home/user/project/restart_server.sh) in your crontab, otherwise, the system won't find your file.
  2. Environment Variables: Cron has a very limited environment. It doesn't load your .bashrc or custom PATHs. If your script relies on specific tools, use their full paths (e.g., /usr/bin/pgrep instead of pgrep).
  3. Missing Shebang: If you forget #!/bin/bash at the top of your script, the system won't know which shell to use to parse your commands, often leading to execution errors.

FAQ

Q: Can I run my script as root? A: Yes, but only if you add the task to the root user's crontab (sudo crontab -e). Be cautious: scripts running as root have full system access.

Q: How do I stop the script from spamming my logs? A: You can modify the script to only log when a restart actually occurs, rather than logging every successful check.

Q: Does the script need to be executable? A: Yes. Without the executable bit set, the cron daemon will fail to trigger the script.

Recap

In this lesson, we advanced our web server project by:

  1. Writing a robust shell script with proper logging.
  2. Ensuring the script is executable.
  3. Scheduling the script with cron using absolute paths.
  4. Verifying the automation output via log inspection.

By moving from manual tasks to automated scripts, you are effectively hardening your server's operational efficiency.

Up next: We will dive into Package Management Basics to learn how to keep your server software updated and secure.

Similar Posts