1

I'm trying to execute a Python script that starts software for my server. The server software must run in a terminal window for user input and status information.

Notes:

  • Server has GUI
  • Ubuntu 19.10
  • folder structure: ~/parent/START_SERVER.py
  • The Python3.8 script requires user input from the terminal

START_SERVER.py (set as executable):

#!/usr/bin/env python3.8
import os
# Several lines of code
os.system('java -jar server.jar')

I've tried:

  1. Adding ./parent/START_SERVER.py in .bashrc in home directory
  2. Adding python3.8 /parent/START_SERVER.py to .bashrc
  3. Adding python3.8 ~/parent/START_SERVER.py to .bashrc
  4. Adding the above commands to Startup Applications
  5. Creating a symbolic link in /etc/profile.d to START_SERVER.py
  6. Doing all of the above with START_SERVER.sh pointing to START_SERVER.py

Behaviors:

  1. 1-5 sometimes results in a popup on login that says "Are you sure you want to proceed?" with one option: "ok"
  2. When 5 is attempted, I get the following on login:

Error found when loading /etc/profile:

/etc/profile.d/START_SERVER.sh: line 3: ----> Outputs line 3 of my file, finding part of a comment that says "./START_SERVER.py"<------ :No such file or directory

/etc/profile.d/START_SERVER.sh: line 6: syntax error near unexpected token `('

/etc/profile.d/START_SERVER.sh: line 6: `def setSettings():'

At this point I'm really at a loss for what to do. How do I get this script to run at login?

1
  • It obviously does run, but there are problems in your shell script. No such file or directory indicates that the file could not be found, probably because the file is not in ./, try using an absolute path instead. The second problem is that def setSettings() is not bash, it looks more like python.
    – danzel
    Mar 4 '20 at 8:40
2

## ~/.config/autostart ##

  1. Open gnome-terminal window.
  2. Create a terminal profile.

    Navigate to 'Edit->Preferences'

    Click '+' next to 'Profiles'

    Name the profile. (in this case I will use "RunComm")

    Click on the 'Command' tab.

    Navigate to the "When command exits" select box and select "Hold the terminal open"

    Click 'Close' to exit

  3. In an editor create a *.desktop file in the ~/.config/autostart directory.

start-server.desktop

[Desktop Entry]
Name=StartServer
Type=Application
Exec=gnome-terminal --working-directory=/home/{YOUR_USER_NAME_HERE}/parent --profile='RunComm' -e 'bash -ci ./START_SERVER.py;bash'
  1. Change file mode of start-server.desktop file. to make it executable. chmod +x ~/home/{YOUR_USER_NAME_HERE}/parent/start-server.desktop

**** I am not sure if step 4 is even necessary. Old habit. I have not tested ****

At user login that should open a gnome-terminal window and execute the python script in bash.

Systemd (at boot)

Create a unit service file for the script. If the directory ~/.local/share/systemd/user doesn't exist then create it. In a terminal with mkdir -p ~/.local/share/systemd/user

In an editor create a file ~/.local/share/systemd/user/startserver.service

[Unit]
Description=Start server

[Service]
RemainAfterExit=no
Type=simple
ExecStart=/usr/bin/python3 /home/{YOUR_USER_NAME_HERE}/parent/START_SERVER.py

[Install]
WantedBy=default.target

Then enable the service

systemctl --user enable startserver.service

It will then start automatically at user login

Status can be checked with

systemctl --user status startserver.service

and

journalctl --user -u startserver.service

systemd

How To Setup Autorun a Python Script Using Systemd

3
  • This is the closest I've come to being successful. I see that it attempted to execute my file, however, I get an EOF error when my file asks for user input in the terminal with the input() function. I assume this may be because there is no terminal opened? What can I add to the startserver.service file to make it open in a terminal window?
    – Craig
    Mar 4 '20 at 19:02
  • This seems to start after a full reboot. What needs to be modified to make it run after a login?
    – Craig
    Mar 4 '20 at 19:10
  • Not to sure at this moment how to invoke a service only at login with systemd. You could use a *.desktop' file in the ~/.config/autostart/` directory. I will update my answer with an example. That would execute at login.
    – da_kingpin
    Mar 5 '20 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.