81

I'm running two cron jobs:

This one executes without a problem:

curl -sS http://example.com/cronjob.php?days=1

But this doesn't run at all:

curl -sS http://example.com/cronjob.php?days=1&month=1

Is this because of the ampersand (&)? If yes, how to pass multiple parameters?

Using argv is not an option.

4 Answers 4

148

You'll notice that this doesn't exactly work in your shell, either.

What you need to do is put single quotes around the URL, like so:

curl -sS 'http://example.com/cronjob.php?days=1&month=1'
3
  • 26
    Windows user running curl binaries should use double-quotes instead of single quotes to get multiple query parameters command working.
    – vivek.m
    Commented Jan 6, 2012 at 15:49
  • 4
    This works for me - many minutes wasted not understanding why my second parameter wasn't working. Commented Feb 25, 2019 at 14:19
  • Welp, I feel stupid now. I was also forgetting my quotes. Thanks man.
    – Rezkin
    Commented Oct 7, 2019 at 22:17
23

As an alternative way, you can use \ before & which is a special character for shell. Generally, & is one of special characters that are meaningful for shell.

So, using a backslash [beside Quoting solution] can be a good solution to this problem. more

In your example you can simply apply this command:

curl -sS http://example.com/cronjob.php?days=1\&month=1
2
  • 4
    ➕1 for actually explaining the issue being & behaving as a special character [which as i looked it up on your link indicates 'background job' 🤷] Commented Nov 1, 2018 at 16:08
  • 1
    this was the exact reason that my curl didn't work. thanks!
    – Sam Lee
    Commented Nov 18, 2019 at 5:44
7

Try a POST Request

curl -d "days=1&month=1" www.example.com/cronjob.php
5
  • 1
    Any particular reason you suggest a POST?
    – SamB
    Commented Jun 5, 2010 at 20:28
  • 2
    No harm in mentioning it, it's good to know (Although I'll go with GET)
    – Yeti
    Commented Jun 5, 2010 at 20:30
  • 1
    well it just up you you also could do it with a GET request. This is just how i would do it. However this would work, and that is the point :-) Commented Jun 5, 2010 at 20:34
  • You can not know if it will work - it depends on the script if it looks POST variables or only GET variables.
    – Kristijan
    Commented Jul 2, 2013 at 11:15
  • on the receiving end it wont matter ($_REQUEST) but using an implicit string as URL not sure how post would work. Using GET or REQUEST will retrieve the variables Commented Apr 23, 2018 at 13:41
0

Here's another way to do a POST request with multiple parameters:

curl www.example.com/cronjob.php -d name=John -d age=30

source: https://spring.io/guides/gs/accessing-data-mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.