Grep stderr to suppress warnings in crontab entry

I needed to setup a crontab entry to restart a node process every week after Let’s Encrypt renewed my certificate. This process is using forever to keep running, and is running as a non-privileged user. I needed to set the path to node, parse the warnings output to remove only the ones I knew were safe to ignore, and ignore stdout from the forever command.

Here’s the crontab entry I used to accomplish this:

# top of crontab file:
NODE_PATH=/home/ec2-user/.nvm/versions/node/v14.7.0/bin/node
...
#crontab entries section:
34 2 * * 0 ec2-user    cd /home/ec2-user/{working-dir} && ${NODE_PATH} /home/ec2-user/.nvm/versions/node/v14.7.0/bin/forever restart server.js >/dev/null 2>/tmp/crontab-forever.txt && grep -v padLevels /tmp/cron-forever.txt | grep -v trace-warnings

The breakdown:

  • Runs on Sunday’s at 2:34 AM
  • Runs as the ec2-user (this is the default user for EC2 instances running Amazon Linux)
  • Changes directories to the working dir for our project
  • Uses the defined NODE_PATH env variable to launch the forever process
  • Send stdout to /dev/null, essentially disregarding any text sent to stdout
  • Sends stderr (2>) to a tmp file
  • Greps the tmp file for padLevels and trace-warnings, which are in the two warnings forever is outputting that are safe to ignore

We don’t want to send stderr directly to /dev/null because if there is an actual error we want cron to email us. We just don’t want an email every week if the output only contains the warnings of which we are already aware.

This article was updated on 2023-02-21

I'm nabeards. I'm a full stack JavaScript developer. I travel full time, a.k.a., Professional Wanderer, a.k.a., Digital Nomad.