Cron job with for loop, executing same command several times to create multiple threads

I’m trying to create a single-line /etc/cron.d/ cron entry, which runs the same command serveral times, essentially to create multiple threads, for long-running jobs:

* * * * * usertorunas  "for i in {1..6} ; do curl -s 'https://www.example.com/path' &>/dev/null & ; done"

What am I doing wrong here? It does not execute, as nothing is shown in ps aux.

Is this perhaps an issue with whatever shell is being used having a different syntax for the for loop? Prefixing SHELL=/bin/bash doesn’t have any effect.

I realise I could just list the same thing 6 times but that seems a pretty cruddy way of doing things.

This is on Ubuntu 22.04.

Asked By: fooquency

||

The outer double quotes should be removed, and it’s an error to use both & and ; to terminate a statement – see for example Using bash "&" operator with ";" delineator?

As well, cron runs jobs using /bin/sh by default which doesn’t support brace expansion or the &> combined redirection – so either replace those with POSIX equivalents:

* * * * * usertorunas  for i in $(seq 1 6) ; do curl -s 'https://www.example.com/path' >/dev/null 2>&1 & done

or set SHELL=/bin/bash before the job

SHELL=/bin/bash
* * * * * usertorunas  for i in {1..6} ; do curl -s 'https://www.example.com/path' &>/dev/null & done
Answered By: steeldriver
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.