0

I tried backing up my Crontab to file using following command: crontab -l > backup/crontab. But this creates only empty file into directory backup and no output is printed to terminal. Exit code of that command is 0.

If I do crontab -l without redirection I get expected output:

/5 * * * * /path/to/script
...

Why is my command failing to write my Crontab contents to file and what should I do the make this work?

I am using Centos 7.7 and following versions of Cron packages installed:

crontabs-1.11-6.20121102git.el7.noarch
cronie-1.4.11-23.el7.x86_64
cronie-anacron-1.4.11-23.el7.x86_64

Shell I use is GNU bash, version 4.2.46.

EDIT:

[user@host ~]$ crontab -l > backup/crontab
[user@host ~]$ ll backup
total 248K
drwx------. 28 user user 4,0K 31.10. 09:30 .
drwxr-xr-x.  7 user user 4,0K  1.10. 12:58 ..
-rw-rw-r--.  1 user user 0    31.10. 09:30 crontab
...

Backup is local directory and has following Selinux policy: staff_u:object_r:user_home_t:s0.

EDIT2:

crontab -l > /tmp/crontab works and other locations were I have write access work too but when target is inside my home directory it fails. I wonder if selinux is causing this.

5
  • Could you do a copy-and-paste of the text in the terminal when you run the command and then do ls -l backup/crontab?
    – Kusalananda
    Oct 31, 2019 at 6:52
  • Is backup a mounted directory? Are there SELinux policies on it?
    – muru
    Oct 31, 2019 at 7:36
  • @Kusalananda I added the output to my post. Oct 31, 2019 at 7:37
  • @muru Backup is local directory and has following Selinux policy: staff_u:object_r:user_home_t:s0. Oct 31, 2019 at 7:44
  • Does it work when you put it elsewhere? e.g. crontab -l > ~/my_crontab
    – FelixJN
    Oct 31, 2019 at 7:46

1 Answer 1

-3

To faithfully redirect console output, save the output as a string, using quotes to absolutely preserve the contents, like so:

echo "$(crontab -l)" > backup/crontab

or, as per comments (which I didn't think through fast enough, apologies!)

printf "%s\n" "$(crontab -l)" > backup/crontab

This prevents the redirected output from being interpreted in any way... for example, the forward slash at the beginning of your crontab example. Note that "echo -e" will interpret escape sequences - eg. chars like \n, \r, \t etc. - so use "echo" without the switch.

On most systems the simple redirect (crontab -l > filename), as you use, should be just fine.

10
  • 3
    The way the user originally redirected the output of crontab -l to a file looks correct to me. The shell would not interpret any part of the output. With your code, however, the echo utility may well interpret the data, and, depending on how the shell is configured, do things like replacing \t with literal tabs etc.
    – Kusalananda
    Oct 31, 2019 at 6:50
  • With this contents of my Crontab get correctly saved to file. On other devices simple redirection works. I wonder why on this particular computer I this is needed. Oct 31, 2019 at 7:48
  • @Kusalananda and Madoc: Absolutely agree, I also backup using simple crontab -l > <file> and it works just fine. Not sure why this use-case fails. Echo will only interpret "escaped" chars with -e CLI switch? (I left off -e so that "escaped" contents would not be interpreted). My assumption was that a crontab -l should not include any weird contents anyway, so echo "" should be good way to hack correct output.
    – sarlacii
    Oct 31, 2019 at 7:55
  • 1
    @sarlacii printf "%s\n" "$(crontab -l)" won't interpret anything
    – muru
    Oct 31, 2019 at 9:03
  • 1
    Well, $() removes all trailing newline characters, printf "%s\n" adds exactly one. Empty lines at the end will disappear. This shouldn't change the logic of the file, still it's not necessarily "the same as crontab -l >backup/crontab". Oct 31, 2019 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .