How would I use a cron job to send an HTML GET request?

前端 未结 4 649
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 14:28

I would like to set up a cron job which sends an http request to a url. How would I do this? I have never set up a cronjob before.

4条回答
  •  Happy的楠姐
    2021-01-22 15:01

    Most probably you want do do this as a normal (i.e. non-privileged) user, so assuming that you are logged in as non-root user:

    $ echo 'curl -s http://example.com/ > /dev/null' > script.sh
    $ chmod +x script.sh
    $ crontab -e
    

    Add this entry to do the http request once every 5 minutes:

    */5 * * * * /path/to/your/script.sh
    

    Preferably you don't want script.sh to give any output when there is no error, hence the > /dev/null. Otherwise cron will email the output to (most likely root) every 5 minutes.

    That should be enough to get you started. At this point it's good if you invest some time to learn a bit about cron. You'll do well bu reading the appropriate man page for cron:

    Start with the format for driving the cron jobs:

    $ man 5 crontab
    

    Then with the actual daemon:

    $ man cron
    

    General advice: make it a habit of reading the man page of any new unix tools that you encounter, instead of immediately copying and pasting command line snippets, spend some time reading what the switches do.

提交回复
热议问题