Paulon8080
Posts: 89
Joined: Wed Jan 04, 2017 12:15 pm

Python + Crontab with TTY?

Sun Jan 19, 2020 1:56 pm

Hey everybody,

I hope this is the right place to post this question, if not apologies.

I have a python project that when run it outputs text to the terminal, however, I need to use the tty terminal (not LX terminal). I can open get this terminal by pressing ctrl-alt-F1, and I can then change the font size with

Code: Select all

sudo dpkg-reconfigure console-setup
.

Once I have done this i can just run

Code: Select all

python myscript.py
and everything runs fine.

My issue is that I would like to iterate through the above process from boot. I'm pretty sure it is easy enough to use crontab to crontab to run the actual python script but I'm not sure how to get it running automatically via tty and also change the text size automatically.

Any advice would be great.

Thanks!
Last edited by Paulon8080 on Sun Jan 19, 2020 10:38 pm, edited 1 time in total.

pcmanbob
Posts: 7957
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python + Crontab with TTY?

Sun Jan 19, 2020 2:40 pm

So Ctrl + Alt + F1 actually exits the GUI.

cron natively runs programs in the background so you don't have any output to a display

the normal way of doing what you want would be to use LXterminal and the autostart file at /etc/xdg/lxsession/LXDE-pi/autostart.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Python + Crontab with TTY?

Sun Jan 19, 2020 2:46 pm

pcmanbob wrote:
Sun Jan 19, 2020 2:40 pm
So Ctrl + Alt + F1 actually exits the GUI.
No, it doesn't. The GUI is still running.
the normal way of doing what you want would be to use LXterminal and the autostart file at /etc/xdg/lxsession/LXDE-pi/autostart.
OP wants it to run in a console TTY, not in a GUI (LX)terminal.

What he wants to do is quite doable; I do something similar on most of my Pis.

But it is a little bit complicated, so I'm holding off on explaining it until I get a better sense of the level of this thread.
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Paulon8080
Posts: 89
Joined: Wed Jan 04, 2017 12:15 pm

Re: Python + Crontab with TTY?

Sun Jan 19, 2020 10:37 pm

Hey pcmanbob and glowinthedark,

Thanks you both for your replies.

@Glow
OP wants it to run in a console TTY, not in a GUI (LX)terminal.
, yes this is it!

On boot run a specific python script but in console tty and also change the text size pf console TTY.
But it is a little bit complicated, so I'm holding off on explaining it until I get a better sense of the level of this thread
Thanks for the consideration and holding off...tbh, not far off a beginner here...I'm pretty sure I could do the cronjob for the actual python script the entering console tty and changing text size everytime Pi boots....I have no idea!

Any guidance would be great!

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

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 12:13 am

OK, here we go.

First, I'm assuming that the changing of the text font size is something that doesn't survive a reboot, so you have to do it after every boot - and that there's no way to change it so that the change is permanent. Presumably, if there is some way to do it once and for all, some other forum poster will chime in with that information. I am also assuming that the dpkg-reconfigure console-setup command that you use to change the font size requires some user intervention - selecting things off menus and so on. Again, if there is a more automated way to do this, others are welcome to chime in.

Second, you need to check to see if you are auto-login-d on tty1. This changes back and forth from time to time with various Raspbian spins - I can't remember if it is on in the latest/current Raspbian. You can check this with the "who" command or by doing "ps uaxw | grep tty1". The point is that if it is not auto-login-d on tty1, then you need to mess around with systemd to turn that functionality (back) on. I can't tell from my systems whether or not it is on by default, because if it isn't, the first thing I do is turn it on.

If you need further help with this part of it, just let me know.

Next, you will need the program called "Expect". Do "apt install expect".

Now, use the "autoexpect" program to generate a script that will do the sudo dpkg-reconfigure console-setup thing automatically. "man autoexpect" will get you on the right path. Call this script "do_ConsoleFont".

Finally, once you've got all that working, put the following lines into your .profile (at the end):

Code: Select all

case $(tty | sed s:^/dev/::) in
tty1) ./do_ConsoleFont
    python myscript.py;;
esac
Now, reboot and, if you've done everything right, it should "just work".
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Paulon8080
Posts: 89
Joined: Wed Jan 04, 2017 12:15 pm

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 10:20 am

Hey GlowInTheDark,

***I edited this as at first it wasn't working - but this was due to sloppy typos on my part, not any of your code guidance :) ***

Thanks again for your time and patience .
First, I'm assuming that the changing of the text font size is something that doesn't survive a reboot, so you have to do it after every boot - and that there's no way to change it so that the change is permanent
Yes, that's exactly the issue!

I did a bit of looking around and was able to automatically boot to console login using the following:

Code: Select all

Run: sudo raspi-config
Choose option 3: Boot Options
Choose option B1: Desktop / CLI
Choose option B2: Console Autologin
Select Finish, and reboot the pi.
This will automatically boot into console.

As you suggested, I installed expect.

Using the man document I ran the following

Code: Select all

autoexpect -f do_ConsoleFont sudo dpkg-reconfigure console-setup
This allowed me to change the font size before rebooting.

Next I open profile

Code: Select all

nano ~/.profile 
Here I made some changes to your code as in my python script is a particular folder with a text file that it uses when running, so here is what I put

Code: Select all

case $(tty | sed s:^/dev/::) in
tty1) ./do_ConsoleFont
   cd Desktop/MyFolder
    python myscript.py;;
esac
Now when I save and reboot this, on reboot I am automatically brought straight through the

Code: Select all

dpkg-reconfigure console-setup   
process into

Code: Select all

Desktop/MyFolder
and myscript.py runs perfectly :D

Thanks again for the help and very clear and helpful instructions :)

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

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 10:46 am

Well, it sounds like you are on the right track. Welcome to the world of debugging! You're going to have to figure out why it doesn't work. The fact that the "cd" command did run (you say you ended up in the "right' directory), indicates that the code in .profile is being triggered. That's a Good Thing.

First note: It probably wasn't necessary to switch it to "Boot to Console" mode, although, if you're not going to use the GUI, then it does simplify things to turn off the "Boot to Desktop" functionality. But, FWIW, on my systems, I always have it boot to desktop so you end up with the main screen displaying the GUI desktop (and that will be running on tty7), but with also a login as pi on tty1 (i.e., if you switch to tty1, you'll see pi logged in and sitting at a $ prompt). But, either way should work.

Second, I assume that you did test the script generated by "autoexpect" and that it does do the right thing, yes? In order to validate this, you would have to either reboot first or make sure that the font was the "wrong" size, before running the script and validating that the script did the right thing and changed it to the "right" size.

Third, are there any error messages onscreen? Presumably, if the Python script "didn't run", there would be an error message displayed indicating why it didn't run.

And, one more thing, you might try putting a short delay after running the do_ConsoleFont script, just in case it needs time for the change to "take", like this:

Code: Select all

case $(tty | sed s:^/dev/::) in
tty1) ./do_ConsoleFont
   sleep 2
   cd Desktop/MyFolder
    python myscript.py;;
esac
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

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

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 10:47 am

Oh, I see you were editing while I was composing.

Sounds like you got it working.

So, there you go...
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Paulon8080
Posts: 89
Joined: Wed Jan 04, 2017 12:15 pm

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 11:07 am

Doh!

I wasn't sure if I should edit or reply to my own comment - went with edit for what I thought was clarity.

The two typo issues were

Code: Select all

./dy_ConsoleFont
What I didn't realise at first that upon boot an error message was appearing telling me that dy.conoleFont couldn't be found...once I noticed that it was easy to correct. ....the same thin happened with mySSR.pt (a type) again flagged in boot mode.

I've learnt lots in this little excercise ...thanks again :)

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

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 11:47 am

Glad to hear it all worked out! Good to see the forum doing what it is supposed to do.

(BTW, I wasn't criticizing or anything when I said you were editing while I was composing. As I say, it all worked out. But it was kind of funny in that after I posted, I looked back at what I was responding to and saw that it said, obviously, something very different from what I had been responding to. This was a little jarring until I read the note you added saying that it was edited. It was funny because the two mechanisms that the forum normally employs to protect you against this sort of thing, were both defeated by the exact sequence of events here. Those two mechanisms are:

1) If someone puts in a response while you are composing, then when you go to post, you get a message that someone else put in a response and you are given the chance to re-edit your post before posting it. That didn't happen here because you did it as an edit rather than as a new response.

2) If someone edits a post that has is not the last post in the thread, the forum appends a message to that post indicating that it has been edited. What happened here is that you were able to get your edit in before I posted my followup, so the forum software thought your edit was to the last post in the thread, so the message was not put in (by the forum software).

Again, all kind of amusing...
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

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

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 12:20 pm

or you can use systemd
see for example https://www.raspberrypi.org/forums/view ... 25#p898424
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

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

Re: Python + Crontab with TTY?

Mon Jan 20, 2020 12:26 pm

systemd is clearly the wrong solution here.

In general, systemd is a solution in search of a problem.

As put by @tpyo kingg in another thread:
As mentioned, making a systemd unit file is another option, but that is much more complex to figure out, implement, and involves multiple steps.
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Paulon8080
Posts: 89
Joined: Wed Jan 04, 2017 12:15 pm

Re: Python + Crontab with TTY?

Tue Jan 28, 2020 3:11 pm

Hi all,

Sorry to reignite this old thread, but I have encountered an issue!

The project still works fine when I boot using auto expect. However, I need it to stop as I have to get back into my the terminal to access the /boot/config.txt file to rotate the screen!

Any suggestions would be great.

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

Re: Python + Crontab with TTY?

Tue Jan 28, 2020 3:31 pm

Leave the job on tty1 running and press Alt/F2 - you will get a new terminal to login to.

Note: Other readers may suggest Ctrl/Alt/F2, but that is not necessary here.

Alt/F1 will get back to tty1 (where your Python script is running).

This works for any of Alt/F1 through Alt/F6.
GitD's list of things that are not quite ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Paulon8080
Posts: 89
Joined: Wed Jan 04, 2017 12:15 pm

Re: Python + Crontab with TTY?

Tue Jan 28, 2020 3:44 pm

Perfect, thank you!

Return to “General programming discussion”