0

I have written a python script which contains a menu in an infinite loop. My goal is start the script in terminal when a user logs in and start interacting with it. I have created a launcher.sh file which will execute on startup and registered it in crontab. However, when system reboots it gives following error:

1: START
2: POWEROFF
Please make a choice: Traceback (most recent call last):
  File "menu.py", line 27, in <module>
    main()
  File "menu.py", line 16, in main
    choice = input ("Please make a choice: ")
EOFError: EOF when reading a line

Here is my script:

#!/usr/bin/python3
def main():
    while True:
        print("1: START")
        print("2: POWEROFF")
        choice = input ("Please make a choice: ")
        print(choice)
        ... other operations

Here is my launcher.sh file:

#!/bin/sh
# launcher.sh
cd /home/rao/Desktop/project
sudo python3 menu.py
cd /

And crontab line:

@reboot sh /home/rao/Desktop/project/launcher.sh >> /home/rao/Desktop/project/logs/status.log 2>&1

Where am I going wrong?

user1584253
  • 975
  • 2
  • 18
  • 55
  • looks like [related thread](https://stackoverflow.com/questions/17675925/eoferror-eof-when-reading-a-line) – Azat Ibrakov Nov 25 '19 at 17:04
  • try to make a message more complex like `choice = input ("1: START\n2: POWEROFF\nPlease make a choice: ")` – Azat Ibrakov Nov 25 '19 at 17:05
  • 1
    "Start up" and "log in" are two separate events and there is no guarantee that a specific user will log in after boot. Without a log in, there is no session. Without a session, there is no terminal. – tripleee Nov 25 '19 at 17:05
  • @tripleee sorry I didn't clarified, its login – user1584253 Nov 25 '19 at 17:09
  • @AzatIbrakov I have already gone through that thread, but I cant understand the solution – user1584253 Nov 25 '19 at 17:10
  • 1
    But `cron` isn't, and cannot easily be connected to a login session, it runs regardless; and a `@reboot` job will certainly run before any user has had the time to log in. – tripleee Nov 25 '19 at 17:11

2 Answers2

1

Cron jobs are executed non-interactively, that's why it fails to read the user input. They're definitely not the right place for something what should interact with the user.

Since the purpose of your script is to interact with a user, there's no need to run it on system startup when there's no user. You want to run it when a user logs in instead, I guess.

You can put your script into the user's ~/.profile or if you're using bash, put it into ~/.bash_profile or ~/.bashrc.

In case of ~/.profile and ~/.bash_profile, it will be executed every time the user logs in (the later one only if he's using bash).

In case of .bashrc, it'll be executed every time the user opens an interactive terminal.

Check for example this article for details:

~/.profile is the place to put stuff that applies to your whole session, such as programs that you want to start when you log in (but not graphical programs, they go into a different file), and environment variable definitions.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
  • I want the terminal to be started automatically and script within it – user1584253 Nov 25 '19 at 17:14
  • and yes, I want to execute this script when user logs in – user1584253 Nov 25 '19 at 17:14
  • OK, then use `.bash_profile` instead. Anything you put into `.bash_profile` will be executed when a user logs in, you can start even the terminal (with your script) from there. – David Ferenczy Rogožan Nov 25 '19 at 17:16
  • I tried putting the command in .bash_profile but didn't worked – user1584253 Nov 26 '19 at 18:40
  • I saw your answer. Don't you think the X terminal emulator is an X (i.e. graphical) application? It's clearly stated in my answer that `.bash_profile` **is not** the right place for graphical programs. But you never mentioned you need it for X actually, so I expected you don't. You should mention such important fact, moreover when you know it and you read that it's not a suitable solution for such purpose in my answer. We would save time of both of us. – David Ferenczy Rogožan Nov 26 '19 at 20:52
  • launcher.sh contains python script, I mentioned **terminal** in my script – user1584253 Nov 28 '19 at 04:47
0

It worked when I put the following command in Startup Applications

x-terminal-emulator -e "python3 /path-to-script/menu.py"
user1584253
  • 975
  • 2
  • 18
  • 55