乐闻世界logo
搜索文章和话题

How to pass multiple parameters to cron job with curl?

1个答案

1

1. Create the Script File

First, create a script file, such as send_request.sh, and write your curl command into it.

bash
#!/bin/bash # Send a POST request with multiple parameters using curl curl -X POST http://example.com/api -d "param1=value1&param2=value2&param3=value3"

In this example, -X POST specifies the request type as POST, and -d is followed by the parameter list with values separated by &.

2. Grant Execute Permissions to the Script File

Add execute permissions to the script file by running the following command in the terminal:

bash
chmod +x send_request.sh

3. Edit the Cron Job

Edit the cron job list to schedule your script for specific times. Run crontab -e to open the cron editor.

In the editor, add a line to define the execution time and call your script. For example, to run the script at 5:00 AM daily, add:

shell
0 5 * * * /path/to/send_request.sh

Here, 0 5 * * * indicates execution at 5:00 AM daily, and /path/to/send_request.sh should be replaced with your script's actual path.

4. Save and Exit the Editor

Save and close the editor. cron will automatically execute your task at the specified time.

Example Explanation

This example sets up a scheduled task that runs at 5:00 AM daily, using curl to send a POST request to http://example.com/api with three parameters: param1, param2, and param3.

Adjust parameters by modifying the script file or update the cron schedule to meet different requirements.

Important Notes

  • Ensure your server can access the target URL.
  • For complex requests, add error handling logic to the script.
  • In production environments, protect sensitive information (e.g., API keys) appropriately.
2024年7月26日 22:57 回复

你的答案