Cron App is a simple command-line utility written in Go that allows you to schedule and execute commands at specified intervals using the cron syntax. This README provides an overview of the project, how to build and run it, and how to configure scheduled tasks.
Before you can build and run Cron App, you'll need the following:
- Go: Make sure you have Go installed on your system.
To build the project, follow these steps:
-
Clone the repository to your local machine:
git clone https://github.com/muhfaris/cron-app.git
-
Change to the project directory:
cd cron-app -
Build the application:
go build -o cron-app
You can run Cron App using the following command:
./cron-appTo run your Go application as a systemd service on a Linux system, you can create a systemd service unit file. This allows you to manage your application as a service, enabling automatic startup, monitoring, and graceful shutdown. Here's how you can set up a systemd service for your Go application:
-
Move cron-app to /usr/local/bin
sudo mv cron-app /usr/local/bin/cron-app
-
Create a systemd Service Unit File:
Open a terminal and create a systemd service unit file with a
.serviceextension, such ascron-app.service. You should typically place this file in the/etc/systemd/system/directory, but you can also use/etc/systemd/user/for user-specific services. Use a text editor or a command-line tool to create the file, for example:sudo nano /etc/systemd/system/cron-app.service
-
Define the Service Unit:
Add the following content to your
cron-app.servicefile, replacing the placeholders with the appropriate values:[Unit] Description=Cron App Service After=network.target [Service] ExecStart=/usr/local/bin/cron-app WorkingDirectory=/home/muhfaris/.config/cron-app Restart=always RestartSec=3 Environment=CONFIG_PATH=config.json StandardOutput=syslog StandardError=syslog [Install] WantedBy=multi-user.targetDescription: A description for your service.ExecStart: The path to your Go application's executable.WorkingDirectory: The directory where your application should run.Restart: Specifies when the service should be restarted (in this example, always).RestartSec: The time to wait before restarting the service.Environment: You can set environment variables if your application requires them.StandardOutputandStandardError: Redirects standard output and standard error to syslog for logging.
-
Reload systemd Configuration:
After creating the service unit file, reload the systemd configuration to make it aware of the new service:
sudo systemctl daemon-reload
-
Start and Enable the Service:
Start the service and enable it to start at boot:
sudo systemctl start cron-app sudo systemctl enable cron-app -
View Service Status and Logs:
You can check the status of your service with:
sudo systemctl status cron-app
example response:
● cron-app.service - Cron App Service Loaded: loaded (/etc/systemd/system/cron-app.service; disabled; preset: enabled) Active: active (running) since Wed 2023-09-13 07:18:32 WIB; 4s ago Main PID: 561435 (cron-app) Tasks: 9 (limit: 18715) Memory: 1.6M CPU: 6ms CGroup: /system.slice/cron-app.service └─561435 /usr/local/bin/cron-app Sep 13 07:18:32 ichiro systemd[1]: Started cron-app.service - Cron App Service. Sep 13 07:18:32 ichiro cron-app[561435]: app-cron: "ts"="2023-09-13 07:18:32.364736" "level"=0 "msg"="starting app" Sep 13 07:18:32 ichiro cron-app[561435]: app-cron: "ts"="2023-09-13 07:18:32.364883" "level"=0 "msg"="list crons" "schedule"="*/1 * * * *" "command"="docker exec -i mongodb1 ls" Sep 13 07:18:32 ichiro cron-app[561435]: "ts"="2023-09-13 07:18:32.365073" "level"=0 "msg"="start" Sep 13 07:18:32 ichiro cron-app[561435]: "ts"="2023-09-13 07:18:32.365111" "level"=0 "msg"="schedule" "now"="2023-09-13 00:18:32.365104403 +0000 UTC" "entry"=1 "next"="2023-09-13 00:19:00 +0000 UTC"To view the application's logs, you can use the
journalctlcommand:journalctl -u cron-app
Your Go application should now be running as a systemd service, and it will start automatically at boot and restart in case of crashes. You can manage the service using standard systemd commands like start, stop, restart, and status.
Cron App uses a configuration file to define the scheduled tasks. The default configuration file is located at ~/.config/cron-app/config.json. You can customize the configuration by editing this file.
The configuration file has the following structure:
{
"jobs": [
{
"schedule": "*/5 * * * *", // Cron schedule expression
"command": "echo 'Hello, World!'" // Command to be executed
},
{
"schedule": "0 0 * * *",
"command": "backup.sh"
}
// Add more scheduled tasks here
]
}-
schedule: This field specifies the cron schedule expression that determines when the command will be executed. You can use standard cron syntax to define the schedule. -
command: The command to be executed at the scheduled time. You can specify any valid shell command or script here.
After editing the configuration file, you can save to apply the changes. The application will automatically reload the configuration when it detects changes to the file.
Cron App will load the scheduled tasks from the configuration file and execute them according to the defined schedules. The application runs in the background and continues to execute tasks at their scheduled times.
You can stop the application gracefully by sending a SIGINT (Ctrl+C) or SIGTERM signal to it. This will trigger a shutdown, and the application will stop executing tasks.
Contributions to Cron App are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you'd like to contribute code, feel free to fork the repository and submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.