alseambusher/crontab-ui

Reduce command length: Error: command too long; while reading crontab for user root

LordMike opened this issue · 2 comments

Make sure these boxes are checked( - [x] ) before submitting an issue.

  • Running latest npm and node? (Run npm --version and node --version). Get the latest nodejs here.
  • The node_modules folder has right permissions (Run ls -ld $(npm root -g)/crontab-ui)
  • Read issues.md
  • Gone through existing open and closed issues.

Hey,

I had a cron job that started failing, and I found out why. The error in crons logs was Error: command too long; while reading crontab for user root. Turns out cron has a length limit on commands that is 999 chars. My command is about 370 chars, but with error/stdout logging, the command in cron becomes 1.038 chars.

I'm thinking that crontabui should warn about this, refuse to write out the file, or maybe there is a way to limit the length of the command?

My command is this:

kopia snapshot create /var/lib/docker/volumes/web-nemligmbwarezdk_db/ /var/lib/docker/volumes/postgresql_pgadmin-data/ /var/lib/docker/volumes/queue_queue-data/ /var/lib/docker/volumes/homeassistant_node-red-data/ /var/lib/docker/volumes/webghmonitormbwarezdk_appdata/ && curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/REDACTED;

Crontabui makes that this:

0 1 * * 0 ((({ kopia snapshot create /var/lib/docker/volumes/web-nemligmbwarezdk_db/ /var/lib/docker/volumes/postgresql_pgadmin-data/ /var/lib/docker/volumes/queue_queue-data/ /var/lib/docker/volumes/homeassistant_node-red-data/ /var/lib/docker/volumes/webghmonitormbwarezdk_appdata/ && curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/REDACTED; } | tee /var/spool/cron/crontabs/32MimKQOvHpX1PwJ.stdout) 3>&1 1>&2 2>&3 | tee /var/spool/cron/crontabs/32MimKQOvHpX1PwJ.stderr) 3>&1 1>&2 2>&3); if test -f /var/spool/cron/crontabs/32MimKQOvHpX1PwJ.stderr; then date >> "/mnt/systems/crontabui/data/logs/32MimKQOvHpX1PwJ.log"; cat /var/spool/cron/crontabs/32MimKQOvHpX1PwJ.stderr >> "/mnt/systems/crontabui/data/logs/32MimKQOvHpX1PwJ.log"; fi; if test -f /var/spool/cron/crontabs/32MimKQOvHpX1PwJ.stdout; then date >> "/mnt/systems/crontabui/data/logs/32MimKQOvHpX1PwJ.stdout.log"; cat /var/spool/cron/crontabs/32MimKQOvHpX1PwJ.stdout >> "/mnt/systems/crontabui/data/logs/32MimKQOvHpX1PwJ.stdout.log"; fi

I have the following:

  • I could of course move the command to a script, and run that, but I like having it all in one place :)
  • I can also reduce my command by doing the below, but that's out of scope
  • The following paths can be replaced with vars:
    • Your /var/spool/cron/crontabs, saves 24x6 chars (minus a var)
    • "Your" /mnt/systems/crontabui/data/logs, saves 32x4 chars (minus a var)
  • Reduce the length of the unique id's.. This could simply be an incrementing number, as crontabui doesn't have a user system or a lot of jobs - in most cases I guess. If you base-64 encoded an incrementing number for each job, the first 64 jobs will all fit in a single char. stderr and stdout in the temp files can also be reduced, saving 2x 5 chars there.
  • Maybe the logic can be reduced, simplifying the wrapping that needs to be done
    • I don't know how expressive cron is, but I think it could be possible to move all the logging logic into a function somehow, and call that with an id and a command.

Example command:

LOGT=/var/spool/cron/crontabs
LOGS=/mnt/systems/crontabui/data/logs
0 1 * * 0 ((({ kopia snapshot create /var/lib/docker/volumes/web-nemligmbwarezdk_db/ /var/lib/docker/volumes/postgresql_pgadmin-data/ /var/lib/docker/volumes/queue_queue-data/ /var/lib/docker/volumes/homeassistant_node-red-data/ /var/lib/docker/volumes/webghmonitormbwarezdk_appdata/ && curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/REDACTED; } | tee $LOGT/32MimKQOvHpX1PwJ.stdout) 3>&1 1>&2 2>&3 | tee $LOGT/32MimKQOvHpX1PwJ.stderr) 3>&1 1>&2 2>&3); if test -f $LOGT/32MimKQOvHpX1PwJ.stderr; then date >> "$LOGS/32MimKQOvHpX1PwJ.log"; cat $LOGT/32MimKQOvHpX1PwJ.stderr >> "$LOGS/32MimKQOvHpX1PwJ.log"; fi; if test -f $LOGT/32MimKQOvHpX1PwJ.stdout; then date >> "$LOGS/32MimKQOvHpX1PwJ.stdout.log"; cat $LOGT/32MimKQOvHpX1PwJ.stdout >> "$LOGS/32MimKQOvHpX1PwJ.stdout.log"; fi

This command, where you put in the common paths, reduces the original 1.038 chars to 788 chars.

Mike.

Good idea.

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.