K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

How to automate a command?

Mon Jul 06, 2020 3:47 am

Can someone walk me through the steps to automate the command sudo apt-get update using python

I want to run the python script by double clicking on a file or icon on the desktop

Talk in for dummies language please, I am sure there is a tutorial some place I have no clue what to even search for.

Thanks in advance

User avatar
kerry_s
Posts: 760
Joined: Thu Jan 30, 2020 7:14 pm

Re: How to automate a command?

Mon Jul 06, 2020 5:21 am

why python?

you can just go ' menu ->preference-> menu editor ', create the app & add to desktop.
you just pick a name, put the command, select an icon & don't forget to select run in terminal..


go to /usr/share/applications in the file manager, copy 'package updater' to desktop.

jahboater
Posts: 5806
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: How to automate a command?

Mon Jul 06, 2020 7:25 am

import os

os.system("sudo apt update")
Pi4 8GB running PIOS64

GlowInTheDark
Posts: 702
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to automate a command?

Mon Jul 06, 2020 8:26 am

Aren't we missing the point here?

OP wants to "automate" running the command. I take that to mean making it so that it (apt-get update) doesn't block waiting for user input. This, incidentally, turns out to be a more complicated undertaking than you might, at first glance, think it to be.

I would know how to do it with Expect - it's kinda been on my back-burner to-do list for a few years now.

I think you can do more or less the same thing in "Python" - I've heard that there is an "Expect-like" package for "Python".
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
DougieLawson
Posts: 39201
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to automate a command?

Mon Jul 06, 2020 8:54 am

K Research wrote:
Mon Jul 06, 2020 3:47 am
Can someone walk me through the steps to automate the command sudo apt-get update using python
https://wiki.debian.org/UnattendedUpgrades
The stuff is already there, you just need to activate it. Don't use that on anything that you consider vital, one day automated updates WILL install something that breaks your system.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

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

Re: How to automate a command?

Mon Jul 06, 2020 9:20 am

GlowInTheDark wrote:
Mon Jul 06, 2020 8:26 am
I take that to mean making it so that it (apt-get update) doesn't block waiting for user input.
apt update almost never waits for user input. The only time that happens is when something very unusual happens (such as when Buster was released early, then changed status from 'testing' to 'stable').
I think that one occurrence is the only time I've ever had to provide input to apt update (or apt-get update) in the decades I've used Debian based systems.
Unreadable squiggle

GlowInTheDark
Posts: 702
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to automate a command?

Mon Jul 06, 2020 11:55 am

apt update almost never waits for user input.
I think you are right.

But I think most of us assumed that OP really meant update/upgrade - which, to most people (not most people on this board, mind you, but most people "out there") is more or less an atomic operation.

So, I think most of us were assuming OP was really talking about the (dist- or full-) upgrade part.

And, just to clarify, no, the "-y" option on "apt-get dist-upgrade" doesn't help much. The problem is that even with that switch, it can still pause - when it needs to ask the user whether or not it is OK to over-write this file or sometimes just because there is a message onscreen (about some new version of something or other) that the developer thought was important for the user to see. Again, "-y" doesn't stop that behavior.

That said, at some point, I will look into the link DL provided. I wonder if that is truly unattended (i.e., totally automated). And then, of course, there is DL's caveat - which suggests that maybe fully automating this isn't a good idea - except for the fact that most users (again, not most users here on this board, but "out there") have no idea what these prompts mean or what they should do other than just "Click OK" (i.e., the text mode equivalent thereof). So, for most people, fully automated is probably OK.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

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

Re: How to automate a command?

Mon Jul 06, 2020 12:48 pm

GlowInTheDark wrote:
Mon Jul 06, 2020 11:55 am
And, just to clarify, no, the "-y" option on "apt-get dist-upgrade" doesn't help much. The problem is that even with that switch, it can still pause - when it needs to ask the user whether or not it is OK to over-write this file or sometimes just because there is a message onscreen (about some new version of something or other) that the developer thought was important for the user to see. Again, "-y" doesn't stop that behavior.
Hmm, I thought it did. However, I'll take your word that it doesn't.

There is always the "--force-yes" option for "apt-get dist-upgrade", which will carry one regardless of what damage may be caused. It is not recommended.
Unreadable squiggle

LTolledo
Posts: 3500
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: How to automate a command?

Mon Jul 06, 2020 1:06 pm

one may update as much as one wants....... but not do an upgrade

if update downloads "something" that might break the system, without invoking the "upgrade" will it still break the system?
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

sparkie777
Posts: 145
Joined: Tue Nov 27, 2012 4:37 am

Re: How to automate a command?

Mon Jul 06, 2020 1:12 pm

GlowInTheDark wrote:
Mon Jul 06, 2020 11:55 am
And, just to clarify, no, the "-y" option on "apt-get dist-upgrade" doesn't help much. The problem is that even with that switch, it can still pause - when it needs to ask the user whether or not it is OK to over-write this file or sometimes just because there is a message onscreen
that's not quite true.

just prepend 'DEBIAN_FRONTEND=noninteractive' to set the environment and make it *really* non-interactive

Code: Select all

DEBIAN_FRONTEND=noninteractive apt -y upgrade
DEBIAN_FRONTEND=noninteractive apt -y dist-upgrade
DEBIAN_FRONTEND=noninteractive apt -y update
DEBIAN_FRONTEND=noninteractive apt -y install <your-favorite-package>

GlowInTheDark
Posts: 702
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to automate a command?

Mon Jul 06, 2020 1:22 pm

that's not quite true.
Thank you for verifying and validating that it IS true. BTW, I hit this, accidentally, a long time ago. I did the upgrade with -y, hit pauses in the upgrade, and was surprised that -y didn't squelch the pausing.

Without your DEBIAN... environment variable(s), -y (by itself) does not inhibit the pauses. On this, we agree.

Note that this is hard to test, because you never know whether any given upgrade will involve pausing (most don't), but when you do it, it is done, so not repeatable. And if you do use the env var (or follow DL's idea/link), you can't tell whether the reason it didn't pause was because of that or just because there were no pauses in this particular run.

To test it, you'd have to set up two identical systems, and upgrade both at the same time, one with using the env var (or DL's idea) and one not. And then keep running this same experiment over an extended period of time, until you hit an upgrade that involves a pause. Quite intricate...
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

sparkie777
Posts: 145
Joined: Tue Nov 27, 2012 4:37 am

Re: How to automate a command?

Mon Jul 06, 2020 1:29 pm

GlowInTheDark wrote:
Mon Jul 06, 2020 1:22 pm
Note that this is hard to test, because you never know whether any given upgrade will involve pausing (most don't), but when you do it, it is done, so not repeatable.
I 'test' it since years with my automated scripts using the 'DEBIAN_FRONTEND=noninteractive' thing. I did not alter anything within the apt configuration file framework. What can I say? It simply works picking some reasonable defaults rather than asking annoying questions.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to automate a command?

Mon Jul 06, 2020 2:11 pm

K Research wrote:
Mon Jul 06, 2020 3:47 am
Can someone walk me through the steps to automate the command sudo apt-get update using python

I want to run the python script by double clicking on a file or icon on the desktop

Talk in for dummies language please, I am sure there is a tutorial some place I have no clue what to even search for.

Thanks in advance


Perhaps it would be useful to take a step back and ask why you want to automate this with a Python script?

Is it because the shell (Terminal/Command Line) prompt is 'scary'?

If so you could use the Add/Remove Software tool under Preferences in the Raspberry Pi Desktop GUI. That tool has a Check for Updates feature accessed from the Options menu tab.

User avatar
DougieLawson
Posts: 39201
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to automate a command?

Mon Jul 06, 2020 2:13 pm

LTolledo wrote:
Mon Jul 06, 2020 1:06 pm
one may update as much as one wants....... but not do an upgrade

if update downloads "something" that might break the system, without invoking the "upgrade" will it still break the system?
True. I want to be here and awake when the upgrades get done, there's three of my RPis that do something useful that's a pain to fix if broken.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

Re: How to automate a command?

Mon Jul 06, 2020 2:22 pm

jahboater wrote:
Mon Jul 06, 2020 7:25 am
import os

os.system("sudo apt update")
I already have the command part I found elsewhere, now walk me through the steps to create the .py then turn the Python code into a icon my desktop that I can double click and it will run the Python code and update my Pi I will add the sudo apt upgrade etc. later.

I start Thonny I enter the above commands then I ?

I have spent at least 20 hrs watching videos on YouTube on Python programming, none of the tutorials ever work out of the box because the instructors are giving instructions as if I have been using a IDE since they were invented , like this video https://www.youtube.com/watch?v=rfscVS0vtbw

Once I wasted about 4 hrs trying to figure out why a simple command I was suppose to just cut and paste into Terminal would not work come to find out the author put a PERIOD at the end of the command os.system("sudo apt update"). <------------
was like the 3rd time I ever entered text in Terminal

Another example they have you open programs like vim and start coding ASSuming I have vim installed, I don't, then NEVER reply when you ask questions.

I learn by doing not by reading or watching video tutorials, I learn faster with written tutorials for newbies, videos get the creative juices flowing I spend a lot of time pausing videos and typing ideas into Cherrytree for future projects.

I really have no real need to automate sudo apt-get update but its something I can relate too while learning the whole process involved in creating the automation of sudo apt-get update, I have a very long list of other things I want to create based off sudo apt-get update

Thanks in advance.

K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

Re: How to automate a command?

Mon Jul 06, 2020 2:38 pm

kerry_s wrote:
Mon Jul 06, 2020 5:21 am
why python?

you can just go ' menu ->preference-> menu editor ', create the app & add to desktop.
you just pick a name, put the command, select an icon & don't forget to select run in terminal..


go to /usr/share/applications in the file manager, copy 'package updater' to desktop.
Python so I can learn, build off the sudo apt update automation process, I learn faster easier by doing.

I like the create the app thing will use that in the future.

Thanks.

K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

Re: How to automate a command?

Mon Jul 06, 2020 2:41 pm

GlowInTheDark wrote:
Mon Jul 06, 2020 8:26 am
Aren't we missing the point here?

OP wants to "automate" running the command. I take that to mean making it so that it (apt-get update) doesn't block waiting for user input. This, incidentally, turns out to be a more complicated undertaking than you might, at first glance, think it to be.

I would know how to do it with Expect - it's kinda been on my back-burner to-do list for a few years now.

I think you can do more or less the same thing in "Python" - I've heard that there is an "Expect-like" package for "Python".
So I google Expect all I get is definitions, totally lost here.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to automate a command?

Mon Jul 06, 2020 3:02 pm

K Research wrote:
Mon Jul 06, 2020 2:22 pm

I already have the command part I found elsewhere, now walk me through the steps to create the .py then turn the Python code into a icon my desktop that I can double click and it will run the Python code and update my Pi I will add the sudo apt upgrade etc. later.

I start Thonny I enter the above commands then I ?


Then you save the file and exit the editor.

Test your code from the shell prompt:

Code: Select all

python3 my_update_app.py

When it is proven to work, add a 'magic code' as the first line of the script to tell the system that it is to run script with the Python3 interpreter:

Code: Select all

#!/usr/local/bin/python3
and
tell the system to treat this file as 'executable':

Code: Select all

chmod +x my_update_app.py 
Now you can run it directly from the shell prompt by typing:

Code: Select all

./ my_python_app.py


I don't know about desktop icons. Over to someone else to pick up the baton when you get that far.




And just to add that you are not alone - there is a near-identical recent thread on a very similar topic here - viewtopic.php?f=91&t=279261

User avatar
neilgl
Posts: 2199
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: How to automate a command?

Mon Jul 06, 2020 4:10 pm

For beginner pi and python stuff, get the V3 beginners guide from here (free) https://magpi.raspberrypi.org/books

K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

Re: How to automate a command?

Mon Jul 06, 2020 5:39 pm

rpdom wrote:
Mon Jul 06, 2020 9:20 am
GlowInTheDark wrote:
Mon Jul 06, 2020 8:26 am
I take that to mean making it so that it (apt-get update) doesn't block waiting for user input.
apt update almost never waits for user input. The only time that happens is when something very unusual happens (such as when Buster was released early, then changed status from 'testing' to 'stable').
I think that one occurrence is the only time I've ever had to provide input to apt update (or apt-get update) in the decades I've used Debian based systems.
Na I can avoid that with the -y thing sudo apt-get update -y ?

K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

Re: How to automate a command?

Mon Jul 06, 2020 5:43 pm

GlowInTheDark wrote:
Mon Jul 06, 2020 11:55 am
apt update almost never waits for user input.
I think you are right.

But I think most of us assumed that OP really meant update/upgrade - which, to most people (not most people on this board, mind you, but most people "out there") is more or less an atomic operation.

So, I think most of us were assuming OP was really talking about the (dist- or full-) upgrade part.

And, just to clarify, no, the "-y" option on "apt-get dist-upgrade" doesn't help much. The problem is that even with that switch, it can still pause - when it needs to ask the user whether or not it is OK to over-write this file or sometimes just because there is a message onscreen (about some new version of something or other) that the developer thought was important for the user to see. Again, "-y" doesn't stop that behavior.

That said, at some point, I will look into the link DL provided. I wonder if that is truly unattended (i.e., totally automated). And then, of course, there is DL's caveat - which suggests that maybe fully automating this isn't a good idea - except for the fact that most users (again, not most users here on this board, but "out there") have no idea what these prompts mean or what they should do other than just "Click OK" (i.e., the text mode equivalent thereof). So, for most people, fully automated is probably OK.
Nope this is what I meant nothing more, don't read any deeper just instruct me step by step how to achieve the below.
Can someone walk me through the steps to automate the command sudo apt-get update using python

I want to run the python script by double clicking on a file or icon on the desktop


Thanks, and thanks for looking out for a fellow member and using your skills to anticipate much appreciated, good reading.

K Research
Posts: 56
Joined: Wed Jun 19, 2019 12:45 am

Re: How to automate a command?

Mon Jul 06, 2020 5:49 pm

sparkie777 wrote:
Mon Jul 06, 2020 1:12 pm
GlowInTheDark wrote:
Mon Jul 06, 2020 11:55 am
And, just to clarify, no, the "-y" option on "apt-get dist-upgrade" doesn't help much. The problem is that even with that switch, it can still pause - when it needs to ask the user whether or not it is OK to over-write this file or sometimes just because there is a message onscreen
that's not quite true.

just prepend 'DEBIAN_FRONTEND=noninteractive' to set the environment and make it *really* non-interactive

Code: Select all

DEBIAN_FRONTEND=noninteractive apt -y upgrade
DEBIAN_FRONTEND=noninteractive apt -y dist-upgrade
DEBIAN_FRONTEND=noninteractive apt -y update
DEBIAN_FRONTEND=noninteractive apt -y install <your-favorite-package>
So adding the 'DEBIAN_FRONTEND=noninteractive' makes it run in a background process where as without the 'DEBIAN_FRONTEND=noninteractive' the same command will run but will run in a Terminal window?

Thanks.

sparkie777
Posts: 145
Joined: Tue Nov 27, 2012 4:37 am

Re: How to automate a command?

Mon Jul 06, 2020 6:58 pm

'DEBIAN_FRONTEND=noninteractive' will just cease from prompting the user. You optionally may want to run this in background (or not).

GlowInTheDark
Posts: 702
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to automate a command?

Mon Jul 06, 2020 10:18 pm

As you can tell from my postings on this thread, I am skeptical of this "non-interactive" thing for doing upgrades. The thing is - it is not always deterministic what the right choice is. I make this choice of words to make the point that even if you are fully tech-savvy and know the choices mean, you might make different choices on different days of the week.

For example (pay attention, b/c this is a bit intricate):

I recently started a thread here about the bogus ejecter applet in Raspbian (specifically, in pcmanfm) and a solution was suggested - that involved commenting out some lines in pcmanfm's config file. So, I did that, but never got around to rebooting to test it (since, presumably, pcmanfm only reads it config file on startup, a restart of pcmanfm would be required and the only way I know to restart it is to reboot).

Then, sometime later, I decided to do a dist-upgrade and one of the things that that did was to note that I had edited that file and it wanted to over-write it with the developer's new version of the file. This is a real hard choice, that has to be made with thought each time it comes up. Note that the default answer to this question is "N" - that means "ignore the new version; stick with my version". But, and this is the key, the most common action that a user will take is to say "Y", which means "overwrite my version with your version".

So, that's the irony. The default action will generally be the most conservative action, that's just the way these things are. But, in actual practice, most often, the user will want to go with the new version. Note that one of the options is "D" - which means "Show me the differences between the old and the new". When I did that, it showed a LOT of differences. This indicates that you'd be taking a real risk if you didn't accept their version of the file (since, presumably, lots of other changes were made in the system that probably depend on the config file being changed accordingly). So, the sensible thing to do is to accept their version.

But think about if this process were to be automated. Which choice would you want the automated/unattended version to make?
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
dickon
Posts: 1553
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: How to automate a command?

Mon Jul 06, 2020 10:48 pm

GlowInTheDark wrote:
Mon Jul 06, 2020 10:18 pm
But, in actual practice, most often, the user will want to go with the new version.
In my very, very long experience of this, I want the thing to leave my version alone and not touch it. I get rather annoyed if things I've fiddled with get broken by package updates, and this doesn't often happen because of the sane defaults Debian chose a couple of decades ago.
But think about if this process were to be automated. Which choice would you want the automated/unattended version to make?
To leave the edited file the hells alone.

Good gods. Reading your posts you'd think this hadn't been tested for. The naivety.

Return to “Raspberry Pi OS”