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

Cronjob / Linux command line question

Wed Dec 16, 2015 12:57 am

Probably super easy.... (I'm from the Windows side. I there's a way to get a Windows command line to select 'yes' in one line like this.)

I use my Pi as a WOL device. Original thread here....
viewtopic.php?t=30393

Add in laziness :roll: and I'm from the Windows side. I like updated and restarted.

I've got my cronjob set up with WOL. I just type cronjob -e to get in to edit it. I've got a bunch of WOL lines set up there.

And then I'll also do these occasionally....
sudo apt-get updates
sudo apte-get upgrades
sudo -reboot

So I'm thinking, why even bother manually doing this? Instead of wakeonlan, I can substitute these lines in and have it upgrade, update, and restart. (Then maybe update again.) Daily. It's probably not going to break anything -- I've got Raspian with a cronjob for wake on lan. That's it. Worst case, I reformat the SD card and start over. If I manually upgrade/update, I can still break something that way doing updates.

Plus, I could have it taking up my precious bandwidth with this in the middle of the night or during the day when I'm at work. (I don't have a super fast connection.)

So the questions....
If I just plug those lines above in a cronjob line in place of wakeonlan, will that work?
For sudo apt-get upgrade sometimes it asks if I want to upgrade y/n. Is there a way to tell it yes? (like sudo apt-get upgrade /y ? Some kind of switch at the end or maybe it could be nested in parentheses type y(sudo apt-get upgrade)


I'm curious just to try it and just to have it as an option. I can always REM (#) it out of the cronjob lines if I don't like it.

It's still doing an upgrade.... I'm not completely sure my apt-get and reboot commands are correct, but I can't cheat and arrow up in the Pi Putty window to see exactly what I typed before.

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:02 am

:roll:

Just this?

sudo apt-get -y upgrade


That will answer yes right away if the upgrades asks a y/n prompt?

http://superuser.com/questions/164553/a ... et-install

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:07 am

Hm. (upgrade finally finished) Looking at my crontab...

00 5 * * * /usr/bin/wakeonlan MACaddress
That would be 5:00am daily.

I'm not sure on the /usr/bin/??? part though.

These are the exact command I'm interested in.... What would I need for the file structure for them? Would /usr/bin/sudo apt-get -y upgrade work? Example...
00 5 * * * /usr/bin/sudo apt-get -y upgrade

sudo apt-get upgrade
sudo apt-get update
sudo reboot

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:11 am

Some interesting stuff here....
http://askubuntu.com/questions/118025/b ... et-upgrade

Combining update and upgrade into one line.

I was wondering how I'd really know it had run though.... There wouldn't be much more for updates/upgrades later if I manually ran it. I noticed the yes/no question on the upgrade and realized it might be stalled out/stuck on that in the background, and I might not be aware of it.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:19 am

rasprm3 wrote::roll:
Just this?
sudo apt-get -y upgrade
That will answer yes right away if the upgrades asks a y/n prompt?
What happens when you cut and paste this into a linux prompt on your pi?

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:38 am

I had already done the upgrade once. I tried it with -y and it looks the same as without. It's already got the upgrade. That's still useful though if I'm typing it in manually. (I don't know what the whole upgrade is... so, sure, do the upgrade... why not?)

The folder path in the crontab makes me hesitate. Might not run. Or it might get stuck/stalled in the background where I'm not aware of it.

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:44 am

Might be something here too.
http://www.techrepublic.com/article/aut ... -cron-apt/

I've got a slow-slow underpowered machine at work running Ubuntu. I see a possibility for this there too. (Does Ubuntu have crontab too?) I click on it to update or upgrade and then have to leave it come back later. It's that slow. But it servers it's last purpose. It's becoming a nuisance updating it like that though.


Looks familiar...

# Automatic package upgrades
0 0 * * 0 root (apt-get update && apt-get -y -d upgrade) > /dev/null



Is root.... taking the place of the folder structure (/usr/bin) or is root the user, like similar to sudo? I'm thinking it's running 'as root' like sudo...

Maybe the Pi crontab doesn't need that folder path. The folder path is just spelling out where wakeonlan is.... I was thinking there might be a Linux equivalent to c:\windows\system32.
Last edited by rasprm3 on Wed Dec 16, 2015 1:49 am, edited 1 time in total.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 1:46 am

You shouldn't automate the update/upgrade operation. One day it will make you sad.

Some reasons why you shouldn't:
There may be a mistake in the update- wait until a particular update is known to be good.
An unsupervised update may fail, and you will have no idea that it happened, or why.
Some updates require user interaction to make choices about the specific system that is being updated.
It is not always necessary to perform an update.

However, it's up to you.

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 2:08 am

http://ubuntuforums.org/showthread.php?t=2004805
0 3 * * * root /usr/bin/apt-get upgrade -q -y >> /var/log/apt/myupdates.log


That's the /usr/bin part. So maybe it would be that easy.

Someday I should brush up on my Linux command line stuff, folder structure.... There's just no payoff for it right now.


My batteries are failing for tonight.... :|

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 3:20 am

rasprm3 wrote:I had already done the upgrade once. I tried it with -y and it looks the same as without.
What looks the same? The system should update without asking you if you are sure. Does it do that?
The folder path in the crontab makes me hesitate. Might not run. Or it might get stuck/stalled in the background where I'm not aware of it.
How about if you set the thing to just download the updates and then later when you are ready you can install them? You said, I think, that you had a slow connection, so this should get around that while not risking doing something that gets stuck you didn't notice.

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 10:32 am

When I did...
sudo apt-get upgrade
vs.
sudo apt-get -y upgrade
... yesterday, I had already it, so running it again, the text it displayed looked the same either way I ran it. Still that same "same" result. I have to wait until another upgrade is available I guess.

I get something like this... (I tried to copy, but copying with the mouse over Putty, it ran the text I copied as a command...)
sudo apt-get - upgrade
Reading package list.... Done
Building dependency tree
Reading state information... Done
The following packages have been kept back: (and then there's a block of text/list)


So I don't think there's any upgrade available yet.



If I just had it download the updates and upgrades and manually ran them later, does it matter if I have the Pi checking for updates and upgrades automatically each day? I wouldn't end up storing an old update or upgrade somehow? Would the latest updates/upgrade overwrite anything old downloaded/waiting to be run?

When I manually run the updates or upgrade I just run it. All the updates, all the upgrades. That generic command. I'm only using the Pi for wake on lan and then connecting with Putty (to tweak the crontab and run updates/upgrades). That's the only thing I'd notice is off. Something could actually be broken now, and I don't notice it. Or, I could run the updates/upgrade manually and break it then. I do watch the text usually while it's updating/upgrading so I might notice if something looked odd. Although the upgrade it did yesterday took too long to watch, maybe over 5 min.

I saw my post about Heartbleed, when I did the first update/upgrade after a year because I didn't think about keeping the Pi updated. The Pi is always online, as long as the router and modem are on. Security is in mind too.

And part is just to use crontab and something with Linux command line again, if crontab is Linux command line (is that Bash?).

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 11:01 am

a few things to note
when putting commandlines into cron it is best to use the full path to everything [for various reasons ]
to find out where a command comes from you can use the command which
i.e.

Code: Select all

which cron
in cron if you have setup the time field right then
20 15 * * *
20 mins past 15 [3 PM] every DOM month DOW

then the command can be
/full/path/to/command /full/path/to/other/things/if/needed

now if you are wanting to run upgrade [which I don't suggest you do ] or anything else that want to use a root command then I suggest yoou use the root account for cron
don't use sudo in cron i.e.
crontab -e
to edit the user crontab
and
sudo crontab -e
to edit the root one
then in the root one you do not need the sudo command
so
sudo crontab -e
20 15 * * * /usr/bin/apt-get update && /usr/bin/apt-get upgrade -y

this will only upgrade if update does not error

this is ignoring the re-directions you can [and should YMMV] use
hope this helps
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 11:15 am

Hm. Thanks. I just use crontab -e to get to my WOL settings. I see there's a different one if I use sudo crontab -e.

Is one for the whole machine and the other is under my user profile?

User avatar
rpdom
Posts: 17217
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 12:21 pm

rasprm3 wrote:Hm. Thanks. I just use crontab -e to get to my WOL settings. I see there's a different one if I use sudo crontab -e.

Is one for the whole machine and the other is under my user profile?
Every user has their own crontab.

The one you get when you use "crontab -e" is the one for your user, yes. The one you get when you "sudo crontab -e" is the one for the root user.

There are special ways of running cron jobs for system stuff, but you shouldn't worry about those for now.

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

Re: Cronjob / Linux command line question

Wed Dec 16, 2015 2:52 pm

Not sure it's the same...

I'm trying the same idea with Ubuntu.

I did sudo crontab -e
Then put in time(unless my time was off) /usr/bin/apt-get update && apt-get upgrade -y


It doesn't look like it ran (could be time formatting though maybe).

How can I tell if it runs for sure? Is there a log? Or I think I can make one somehow.... In Windows in the task scheduler, I'm used to seeing "last run" and "next run" times. That's easy enough to tell if it ran. Otherwise I make my own logs with echo.

Otherwise, I manually ran apt-get update and apt-get upgrade. For upgrade, it's still going so I'm thinking it didn't work with crontab. Or I'm checking it too soon.... It's taking a while to run.

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 1:19 pm

I have same issue

When I run below script directly I going well

Code: Select all

domoticz_backup.sh
but when add below code in crontab nothing happens

Code: Select all

5 11 * * * /domoticz_backup.sh
Is there a log-file? or what is going wrong

User avatar
rpdom
Posts: 17217
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 1:36 pm

rjobaan wrote:I have same issue

When I run below script directly I going well

Code: Select all

domoticz_backup.sh
but when add below code in crontab nothing happens

Code: Select all

5 11 * * * /domoticz_backup.sh
Is there a log-file? or what is going wrong
Is your script really in the root directory ("/")?? That's really not a good place to put it.

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 8:39 pm

No it is not in root

I will adjust the code here

Code: Select all

/home/pi/scripts/domoticz_backup.sh
but when add below code in crontab nothing happens

Code: Select all

5 11 * * * /home/pi/scripts/domoticz_backup.sh
But why is it not working?

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

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 9:11 pm

^^^^ Maybe a permissions thing? Permission to run, permission for access to the file or something it touches? Or something with where/the environment the file is starting in?


I thought I had a fix for my Ubuntu machine issue. Typo in my crontab. Fixed that. Set a new test time. Waited. Then I manually ran it. Updates looked like they only read which was a good sign -- If it's already installed, there's nothing to install. It's only reading and checking. But the upgrade I did manually did upgrade something, so that must not have worked. It might just split the update && upgrade line part, into separate lines. Or stick a sudo reboot on it. Rebooting will make it obvious it's running.

Double checking... I'm using sudo crontab -e for sure.

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

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 9:16 pm

Hm...

So if I'm in sudo crontab -e

and I want to schedule a reboot.....

Manually, I'd use sudo -reboot.
I'm already in sudo with sudo crontab -e though.
So would I just use -reboot ?

ex.
30 15 * * * /usr/bin/ -reboot

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 9:34 pm

I did what you said

First I used

Code: Select all

sudo reboot
my pi rebooted. I could not use sudo -reboot.
After that I added it to crontab
but nothing happens

Code: Select all

25 22 * * * /usr/bin/reboot
I checked date on pi
pi@raspberrypi:/ $ date
Thu 17 Dec 22:31:02 CET 2015
So what can else be the problem?

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

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 9:45 pm

Take two for me.... with /usr/bin/reboot

For me and the other guy, we could have it spit info out to a log. The > ending here. This one goes nowhere though.
0 0 * * 0 root (apt-get update && apt-get -y -d upgrade) > /dev/null

I think (if it's like Windows) > will write over the existing file, just the new info in that file. >> will append to the end of the file if it exists or make a new file if it doesn't exist.

I'm not up to speed with Linux though, so I'm hesitating because I don't know where to point it for the file. And I don't know how to access the file (I'm using Putty) to view the file later.

reboot is just a way I can avoid having to figure out logs, where to point it for a log, how to view the log.....

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

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 9:52 pm

Dang... no luck with my crontab job to reboot. I don't think my crontab jobs are running.

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

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:02 pm

No idea where I picked this up. sudo -reboot

It must have been on my Pi. I think someone showed me that probably. Then I found it in the previous command lines.

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

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:03 pm

On ubuntu

sudo -reboot doesn't work
sudo reboot does work

Return to “Beginners”