rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 3:28 pm

These two lines are very different, aren't they?

sudo crontab -e
vs.
crontab -e


crontab -e is just 'per profile,' just for the one user, right? While 'sudo crontab -e' is for the whole OS, right?

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 3:45 pm

Not really. There isn't really any concept of "The whole OS".

The command:

Code: Select all

sudo crontab -e
which should never be used, edits the crontab file for a specific user, namely "root".
If this post appears in the wrong forums category, my apologies.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 4:00 pm

They're two completely separate files though, aren't they?

I think the sudo one was the only way I could get an update/upgrade automated before. I was looking into it again but it's been a while.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 4:17 pm

rasprm3 wrote:
Thu Sep 28, 2017 4:00 pm
They're two completely separate files though, aren't they?
Yes, although it is probably not such a good idea to think of them as "files", as much as a system functionality where each user has his own crontab functionality. Even though they are usually stored as (separate) files, conceptually, there's no reason why it couldn't all be sorted in a single database.
I think the sudo one was the only way I could get an update/upgrade automated before. I was looking into it again but it's been a while.
No, you could just as easily do it by invoking "sudo" from pi's crontab. Opinions vary on whether or not this is a good idea (some here violently believe that it is not and that people who do it are sub-human life forms), but I personally think it is much easier to keep track of things by having it all in the default user's (i.e., pi's) crontab, than having it spread all over the place.

I also think that what you do in crontab should, to as great a degree as is possible, mirror what you do on the command line (i.e., before you decide to crontab it), and since you frequently see people recommending:

Code: Select all

$ sudo apt-get update
$ sudo apt-get upgrade
$
I.e., with "sudo" on each command line - it then follows that it is more natural to do the same thing when you crontab it.

P.S. It is probably not a good idea to automate the update/upgrade process, tempting as it is to do so...
If this post appears in the wrong forums category, my apologies.

W. H. Heydt
Posts: 11084
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 4:51 pm

Martin Frezman wrote:
Thu Sep 28, 2017 3:45 pm
Not really. There isn't really any concept of "The whole OS".

The command:

Code: Select all

sudo crontab -e
which should never be used, edits the crontab file for a specific user, namely "root".
Well... That dependos. If you want to run a cron job that requires root permissions, putting it in the root crontab would certainly be a logical choice. I use it for (for instance) changing the backlight setting on an RPF display.

User avatar
DougieLawson
Posts: 36554
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 5:15 pm

W. H. Heydt wrote:
Thu Sep 28, 2017 4:51 pm

Well... That dependos. If you want to run a cron job that requires root permissions, putting it in the root crontab would certainly be a logical choice. I use it for (for instance) changing the backlight setting on an RPF display.
Bzzzt! Wrong.

Put a file with a crontab line (single part name, no qualifiers) in /etc/cron.d
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 5:19 pm

It looks like I was messing with update, upgrade, and rebooting before. But with Ubuntu in addition to the Pi, and there were a few slight differences I think.

I found this line in one of my posts. It may have been getting the Pi (or Ubuntu) to reboot with a crontab that pushed me to 'sudo crontab -e.'
time sudo /usr/bin/ap-get update && sudo /sbin/reboot



Found this in one of my posts about Ubuntu...
On my Ubuntu machine,
sudu crontab -e
00 5 * * * sudo /usr/bin/apt-get update && sudo /usr/bin/apt-get upgrade -y && sudo /sbin/reboot

From this thread.
viewtopic.php?f=91&t=129133&p=866899#p866899



I'm going to upgrade to Sketch on my Pi. I just want to copy out whatever settings I current have. It's a very simple Pi set up but still on Wheezy. Basically I just need to look at my Pi's "crontab -e" and "sudo crontab -e" information.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 10:04 pm

DougieLawson wrote:
Thu Sep 28, 2017 5:15 pm

Put a file with a crontab line (single part name, no qualifiers) in /etc/cron.d
This I wouldn't mind knowing a little more about. Can you tell me or point me in the direction of how that works please? You just add files in a folder and cronjobs will run from those?

From the terminal, I'm not even quite sure how to make a simple text file in linux or how to move one for sure. I wouldn't mind knowing that.

Before I was just trying to upgrade/update and restart a Pi or Ubuntu machine. I'm willing to have it mess up for the trade off of always having it updated.

User avatar
DougieLawson
Posts: 36554
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 10:16 pm

sudo nano or sudo vi to make a file owned by root.
nano or vi to make a file owned by pi.

There's also GUI editors, but I've never used one of those.

https://www.linuxquestions.org/question ... bs-853881/ is a good starting point for the various crontabs.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 10:41 pm

Checked my pi at home. I'm doing everything with "crontab -e" there. So it must have been the Ubuntu test machine I was experimenting with before. I think that needed "sudo crontab -e" in order to get it to restart. Or maybe it was the folder path. That must have been as far as I got. My home Pi must not have had restarts automated. The Ubuntu machine wouldn't make sense to restart often though.

W. H. Heydt
Posts: 11084
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: thinking about crontabs, in terms of sudo

Thu Sep 28, 2017 11:00 pm

DougieLawson wrote:
Thu Sep 28, 2017 5:15 pm
W. H. Heydt wrote:
Thu Sep 28, 2017 4:51 pm

Well... That dependos. If you want to run a cron job that requires root permissions, putting it in the root crontab would certainly be a logical choice. I use it for (for instance) changing the backlight setting on an RPF display.
Bzzzt! Wrong.

Put a file with a crontab line (single part name, no qualifiers) in /etc/cron.d
My method may not be "approved", but it isn't actually "wrong" as it works. Like many things in *nix, there are multiple ways of accomplishing this, even if some may be preferred over others. At best, you should have said "not the generally recognized as preferable method."

Return to “Beginners”