DEV Community

Isaac Ntumpi
Isaac Ntumpi

Posted on • Edited on

Hello World Every Minute: Crontab + Live Log Tailing

Image description

When managing Linux systems, automating repetitive tasks is key to efficiency and consistency. With crontab, you can schedule scripts to run at any interval — including every minute. In this short guide, we’ll create a script that simply prints HELLO WORLD, schedule it to run every minute, and log its output. You'll also learn how to monitor it live using tail -f, a powerful command-line tool for real-time log viewing.

[Step 1: Create the Python File]

Open a terminal and create the script file:

nano ~/hello.py
Enter fullscreen mode Exit fullscreen mode

Paste the following code:

#!/usr/bin/env python3
from datetime import datetime

logfile = "/home/$USER/hello.log"  # Replace $USER manually if needed

with open(logfile, "a") as f:
    now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    f.write(f"[{now}] HELLO WORLD\n")

print("HELLO WORLD")

Enter fullscreen mode Exit fullscreen mode

Tip: Replace "/home/$USER/hello.log" with your actual path, for example: "/home/isaac/hello.log"

Then make the file executable:

chmod +x ~/hello.py  #bash
Enter fullscreen mode Exit fullscreen mode

[Step 2: Add the script to crontab and Test and monitor logs live]

Open your user’s crontab:

crontab -e
Enter fullscreen mode Exit fullscreen mode

Add this line at the bottom:

* * * * * /usr/bin/python3 /home/your_user/hello.py
Enter fullscreen mode Exit fullscreen mode

After a minute, check if the log file was created:

ls -l ~/hello.log
Enter fullscreen mode Exit fullscreen mode

Then, use tail -f to monitor the log in real-time:

tail -f ~/hello.log
Enter fullscreen mode Exit fullscreen mode

You should see a new "HELLO WORLD" line appear every minute. Like on below image.

Image description

Conclusion

By following this step-by-step guide, you've learned how to schedule a simple Python script to run every minute using crontab. This script prints and logs a message, giving you a hands-on understanding of how cron jobs, logging, and real-time monitoring with tail -f work together in a Linux environment.

Even though this example is simple—just printing "HELLO WORLD"—the approach is powerful and scalable. You can adapt this pattern to automate tasks like data collection, backups, notifications, or system health checks.

Whether you're a beginner exploring automation or a developer integrating scripts into a production pipeline, mastering cron and logging is a foundational skill. Keep experimenting, build smarter scripts, and level up your DevOps toolbox!

Top comments (0)