Search code examples
bashubuntucurlscriptinggitlab

Curl and run script from crontab


I want to make a cronjob which would get script via curl from gitlab with access token and run it. Everything works when I run it manually but from cron it seems to start and immediately closes root session.

Here's my cronjob

35 11 * * * /usr/bin/curl --request GET --header 'PRIVATE-TOKEN: *********' https://gitlab.example.com/api/v4/projects/2/repository/files/monitor%2Esh/raw?ref=main | bash -

Here's my syslog

Aug 07 11:35:01 CRON[1302479]: (root) CMD (/usr/bin/curl --request GET --header 'PRIVATE-TOKEN: *********' https://gitlab.example.com/api/v4/projects/2/repository/files/monitor)
Aug 07 11:35:01 CRON[1302478]: pam_unix(cron:session): session closed for user root

Header of my script

#!/bin/bash
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin/

I don't get why curl command is truncated in syslog, is it normal behavior? What am I missing? Or its technically impossible to run script via curl like this?

I've tried to send output to log file by appending

>> /var/log/cron_log.log 2>&1

but nothing is written there when cron starts job.


Solution

  • Percent (%) is a special character in crontab entries which start a new line, try escaping it:

    35 11 * * * { /usr/bin/curl --request GET --header 'PRIVATE-TOKEN: *********' "https://gitlab.example.com/api/v4/projects/2/repository/files/monitor\%2Esh/raw?ref=main" | bash -; } >> /var/log/cron_log.log 2>&1
    

    Quotes are also added around the URL.