Passwords and API tokens in cron jobs: who can see them, and how to hide them
A password in a crontab line showed up in cron's log and in ps for every user on our Debian 13 box, even with curl -u. What leaked, what stayed hidden, and why curl -v writes it to your log anyway.
5 min read
The job needs to call an API, so the token goes where it’s handy: right in the crontab line.
* * * * * curl -fsS -u api:s3cret-A https://api.example.com/sync
crontab -l is only readable by you, so it feels private. We tested that. On Debian 13 we ran five
versions of this job as deploy under real cron, against a local server that took three seconds to
answer. A second, unprivileged user, eve, ran ps -eo user,args every 0.2 seconds. Then we
grepped cron’s log and the job logs for the secrets.
debian 13 cron · curl 8.14 · a second user polling ps
- curl -u api:s3cretin ps via sh -c, and in cron's log
- -H "Bearer $TOKEN"expanded token in ps
- curl -vtoken written to the job log
- .netrc, header filenowhere
- runwisp secrets_filelog says [redacted]
Short answer
Keep secrets off the command line. Anything in it is visible to every user through ps while the
job runs, and cron writes the whole line to the system log. Put the secret in a file only the job’s user
can read, and have the program read it: curl --netrc-file ~/.netrc, or a header from a file with
curl -H @file. If the secret has to come from a variable, pipe it in:
printf 'Authorization: Bearer %s\n' "$TOKEN" | curl -H @-. And don’t run scheduled jobs with
curl -v or set -x, which print the secret into the log. RunWisp, an open-source cron replacement
that ships as a single binary, keeps secrets in a secrets_file, hides the values in its UI and
API, and stores [redacted] in the run’s log when a job prints one.
What leaked
| Cron command | ps as another user |
cron’s CMD log line |
Job log |
|---|---|---|---|
curl -u api:s3cret-A … |
yes, via sh -c |
yes | no |
curl -H "Authorization: Bearer $TOKEN" … |
yes, expanded | no, shows $TOKEN |
no |
curl --netrc-file /home/deploy/.netrc … |
no | no | no |
printf '…Bearer %s\n' "$TOKEN_D" | curl -H @- … |
no | no | no |
curl -v -H @/home/deploy/.config/hdr … |
no | no | yes |
The server got the right credentials in all five, so each one “works”. The difference is who else got them.
curl -u hides itself, but not its shell
curl knows passwords on its command line are a problem, so it blanks them in its own process
arguments. eve never saw a curl entry with the password. This is what showed up instead:
deploy /bin/sh -c curl -fsS -u api:s3cret-A http://127.0.0.1:8000/a > /tmp/a.log 2>&1
Cron doesn’t start curl. It starts /bin/sh -c with your whole line, and that shell sits there,
waiting for curl, with the line as its argument. For our three-second request that was plenty of
polls. A slow backup upload would show it for an hour.
Cron also logs every line it runs: to the journal on a stock Debian 13 (journalctl -u cron), or to
/var/log/syslog if rsyslog is installed. Our CMD entry kept the password, and members of the
adm group can read both, so it’s not just root:
CRON[88]: (deploy) CMD (curl -fsS -u api:s3cret-A http://127.0.0.1:8000/a > /tmp/a.log 2>&1)
That line stays until the log rotates out, long after you’ve changed the crontab.
A variable fixes the log, not ps
Moving the token into a variable at the top of the crontab looks like the fix:
TOKEN=s3cret-B
* * * * * curl -fsS -H "Authorization: Bearer $TOKEN" https://api.example.com/sync
Cron’s CMD line now has $TOKEN in it, not the token. But the shell expands $TOKEN before it
starts curl, and curl only knows to hide -u passwords. This is what eve saw:
deploy curl -fsS -H Authorization: Bearer s3cret-B http://127.0.0.1:8000/b
Same goes for any tool you pass --token "$TOKEN". If the secret ends up in a program’s arguments,
it ends up in ps.
What kept it hidden
A file the program reads itself. For curl that’s .netrc:
umask 077
printf 'machine api.example.com login api password s3cret-C\n' > ~/.netrc
* * * * * curl -fsS --netrc-file /home/deploy/.netrc https://api.example.com/sync
For a bearer token, put the whole header in a file and pass it with -H @/path/to/file. Most tools
have an equivalent: mysql --defaults-extra-file, ~/.pgpass for psql and pg_dump, a
--password-file flag on restic. Look for it before you reach for a flag.
A pipe, when the secret is in a variable.
* * * * * printf 'Authorization: Bearer %s\n' "$TOKEN" | curl -fsS -H @- https://api.example.com/sync
printf is built into the shell, so no process ever has the token in its arguments. curl reads the
header from standard input. ps showed nothing for this one, and the CMD line only had $TOKEN.
The file still has to be locked down. Your own crontab is safe: the spool file is
-rw------- deploy crontab. Files in /etc/cron.d are usually 644, which means every user can
cat them. chmod 600 /etc/cron.d/job is fine: cron still read and ran ours.
curl -v writes the token into your log
The fifth job read its header from a file, kept it out of ps and out of cron’s log, and then printed
it anyway:
> Authorization: Bearer s3cret-E
-v prints every request header to stderr, and 2>&1 sent that to the log. set -x in a shell
script does the same thing with every command after expansion. Both are handy while you debug the
job, and both are worth removing before it runs every night into a log file. If you
keep job output in a file, check who can read it.
RunWisp: secrets that stay out of the log
RunWisp, an open-source cron replacement that ships as a single binary, has a separate place for
secrets. A secrets_file is a plain KEY=value file that the daemon reads and injects into the
task’s environment:
umask 077
echo 'TOKEN=s3cret-RW' > /etc/runwisp/api.env
[tasks.ping-api]
cron = "* * * * *"
secrets_file = "/etc/runwisp/api.env"
run = '''
printf 'Authorization: Bearer %s\n' "$TOKEN" | curl -v -fsS -H @- https://api.example.com/sync
'''
We kept -v on to see what it does with it. The run’s stored log, and runwisp logs ping-api,
had this:
> Authorization: Bearer [redacted]
RunWisp masks any secret value a task prints before the line reaches the log, so a forgotten -v
or set -x doesn’t leak it. The keys and values never leave the daemon, so the Web UI and the API
don’t show them, while plain env values stay visible to whoever operates the box, which is what you want
for a URL and not for a token. With the header piped through printf, eve’s ps loop saw
nothing.
Each run’s output is kept, and Ctrl+F searches it in the Web UI, so you can check the request went
out and what came back without the token in it. The
task reference has the
details on secrets and secrets_file.
Common questions
Can other users see the password in my cron job?
Is a variable at the top of the crontab safer than a password in the command?
Where should a cron job read a password from?
Why is my API token in the cron job's log file?
Is there a cron replacement that keeps secrets out of the job's logs?
Read next
-
Cron pipeline exits 0 when pg_dump fails: pipefail and empty backups
pg_dump | gzip in cron reported success while the database was down and left a 20-byte backup. What pipefail fixes, where /bin/sh lacks it, and what it doesn't fix.
-
Retry a failed cron job automatically, without hiding the failure
The retry loop everyone posts runs once under cron's /bin/sh and exits 0. A wrapper that keeps the exit code, plus what curl and systemd retries did in our tests.