crosland
Posts: 37
Joined: Mon Feb 09, 2015 6:13 pm

root crontab not running all commands

Thu Dec 13, 2018 8:10 pm

I am trying to follow instructions in this script https://github.com/aoakley/cotswoldjam/ ... ian-shrink to add a job to roots cron tab.

I am using

Code: Select all

sudo crontab -e
Adding

Code: Select all

@reboot if [ -e /boot/1stboot.txt ]; then rm /boot/1stboot.txt ; /usr/bin/raspi-config --expand-rootfs ; /sbin/shutdown -r now ; fi
to the end of the crontab.

I couldn't get it to work (the .txt is removed, but the filesystem is not being resized so I redirected the output

Code: Select all

/usr/bin/raspi-config --expand-rootfs &> /home/pi/log
I can see the log file is created, and owned by root, but it is empty.

Can anyone help?

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: root crontab not running all commands

Thu Dec 13, 2018 8:36 pm

I did not look closely at your command or how/why it may have failed.

However, ...

There are lots of things that can go wrong when using crontab, and it is best to keep things as simple as possible. You almost never want to have anything the least bit complicated in the crontab file; rather, about all that should be in the crontab file is an invocation of a separate script that you write to do whatever it is you need to do.

Once you get the right work habits for working with crontab, things tend to more or less sort themselves out.

So, I would put that long command into a file, and make the file start with:

Code: Select all

#!/bin/bash
exec > mycronfile.log 2>&1
set -e -x
### rest of command(s) go here 
Make this file executable and put it into your crontab file.

Now, when you run it via cron (i.e., put it into some crontab file), you'll get a log file that you can look at and figure out what went wrong.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

crosland
Posts: 37
Joined: Mon Feb 09, 2015 6:13 pm

Re: root crontab not running all commands

Thu Dec 13, 2018 9:03 pm

Thank you. I think that helps, at least to see the errors :)

Now the logfile contains

Code: Select all

+ '[' -e /boot/1stboot.txt ']'
+ rm /boot/1stboot.txt
+ /usr/bin/raspi-config --expand-rootfs
/usr/bin/raspi-config: 132: /usr/bin/raspi-config: parted: not found
/usr/bin/raspi-config: 133: [: -ne: unexpected operator
/usr/bin/raspi-config: 139: /usr/bin/raspi-config: parted: not found
Please reboot
Is

Code: Select all

/usr/bin/raspi-config: 132: /usr/bin/raspi-config: parted: not found
a problem with the PATH?

I am not a Linux expert so I am not sure whet environment exists when the cron job runs.

Any guidance on the unexpected operator?

PS I can run raspi-config once booted and logged in

crosland
Posts: 37
Joined: Mon Feb 09, 2015 6:13 pm

Re: root crontab not running all commands

Thu Dec 13, 2018 9:22 pm

Actually, that helped a lot, thank you.

I should have tried before posting, but the simple addition of a line to the script to set an appropriate PATH solved the problem.

I guess the original failure was also down to the path not being set.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: root crontab not running all commands

Thu Dec 13, 2018 11:17 pm

Yes - I just checked and parted is in /sbin - and I don't think /sbin is in the default (minimalist) PATH.

Usually, the first thing I do when I start using cron on a new system is setup a job that runs just the "env" command. That gets me a list of the default cron environment. From there, it's usually pretty easy to figure out what to do next.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Return to “Troubleshooting”