Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux Script Sudo and "&"
#1
Hello everyone in the forums of python
i am new to language
and i have make a small script
which i want to run
by using the followed method from linux terminal

sudo python myscript.py &


the problem is that the operating system creates a process ID
but doesnt show the GUI when i do fg command its shows up

also its shows when i remove the "&" char at then end

the script contains a GUI which had been made by a Designer and a code to it
from the autogenerated command pyuic
the rest of the commands is hand-written

any help is appreciate it

thanks everyone
Reply
#2
what is the "&" in the end?

If you want to pass a parameter to the script you can try:

if __name__ == '__main__':
    ......
    if len(sys.argv) > 1:
        myvalue = sys.argv[1]
        .....
sudo python myscript.py parameter
Reply
#3
(Aug-28-2019, 07:25 AM)Axel_Erfurt Wrote: what is the "&" in the end?

If you want to pass a parameter to the script you can try:

if __name__ == '__main__':
    ......
    if len(sys.argv) > 1:
        myvalue = sys.argv[1]
        .....
sudo python myscript.py parameter

the "&" character under Linux operating system is a special one
its gives the ability to "disengange" the process from the terminal
and make the program to run autonomous from the terminal

eg i open the terminal and i want to open firefox

with the command "firefox &" i can close the terminal and the firefox will be up

without the "&" at the end when i close the terminal the firefox will close

same situation with almost every python script
so i am losing something up cause my newbie state
thanks
Reply
#4
Add a shebang line on the top of the script to the path of your intended python interpreter
#!/usr/bin/python3.6
make the script executable
sudo chmod +x myscript.py
and then try the command again
sudo python myscript.py &
On mine i was getting the same issue. It stopped when sudo and & were present in the command.
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [3] 16587 [2]+ Stopped sudo python3.6 test11.py metulburr@ubuntu:~$
but worked after making those same changes
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [4] 17172 metulburr@ubuntu:~$
Recommended Tutorials:
Reply
#5
(Aug-28-2019, 08:32 AM)metulburr Wrote: Add a shebang line on the top of the script to the path of your intended python interpreter
#!/usr/bin/python3.6
make the script executable
sudo chmod +x myscript.py
and then try the command again
sudo python myscript.py &
On mine i was getting the same issue. It stopped when sudo and & were present in the command.
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [3] 16587 [2]+ Stopped sudo python3.6 test11.py metulburr@ubuntu:~$
but worked after making those same changes
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [4] 17172 metulburr@ubuntu:~$

its keeps to do the same
its suppose to
A)To ask the sudo password
B)To open the GUI
Reply
#6
Why do you run your program as root?
Can you start it as normal user?

Sudo runs your programs in a different environment. If you have an activated virtuelenv, then it would be not used with sudo.
If you need access to resources like ttyUSB0, you can add the dialup group (is in many distributions the case) and after a new login, the user has the right to access ttyURB0. If you want to open a port below 1024, it's not possible without root access.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Aug-28-2019, 10:15 AM)DeaD_EyE Wrote: Why do you run your program as root?
Can you start it as normal user?

Sudo runs your programs in a different environment. If you have an activated virtuelenv, then it would be not used with sudo.
If you need access to resources like ttyUSB0, you can add the dialup group (is in many distributions the case) and after a new login, the user has the right to access ttyURB0. If you want to open a port below 1024, it's not possible without root access.

hello i want to run a command with sudo
because from the nature it cant be run as normal user
in fact i have make a subprocess with Popen and PIPES
so this is the problem
i need sudo cause the internal command in subprocess needs sudo
thanks again
Reply
#8
If it is a GUI Application you should not use sudo. Use gksu then you will be asked for the password.
Reply
#9
(Aug-28-2019, 04:18 PM)Axel_Erfurt Wrote: If it is a GUI Application you should not use sudo. Use gksu then you will be asked for the password.

having the python exec command at the beggining of the file as mentioned before
and trying to running via pkexec command
i get no GUI

in fact i noticed the same behaviour

the pkexec dialog popup shows up i enter my password
and then boom
nothing happens the hdd do some job
but nothing

also i noticed the same behavior not only with

"pkexec script.py"

but with

"pkexec script.py &"

so its something with the way the unix system handles the python script
and vise versa

thanks
Reply
#10
if the script is just for you, try that
Quote:echo <password> | sudo -S <command>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 634 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 894 Nov-16-2022, 07:58 PM
Last Post: Winfried
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,278 May-24-2022, 07:02 AM
Last Post: netanelst
  sudo apt Not Working Dacdiver 11 18,270 Nov-09-2021, 05:48 PM
Last Post: jefsummers
  sudo RyAn 2 1,495 Nov-09-2021, 12:43 PM
Last Post: DeaD_EyE
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,535 May-17-2021, 11:29 AM
Last Post: throwaway34
  Error when running script on startup in Linux NoahTheNerd 0 1,926 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  Login to NordVPN on Linux with python script AGreenPig 2 5,912 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  script works in windows but not in linux ovidius80 2 2,679 Apr-29-2020, 02:10 PM
Last Post: ovidius80
  Methods of running a script on Linux distro Vysero 6 3,794 Aug-21-2018, 09:11 PM
Last Post: heras

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020