Skip to content
Advertisement

How to fix wmctrl Cannot open display when Python open subprocess

This is my program, and it works very well.

JavaScript

Output:

JavaScript

But if I want to run this program at startup as root in Linux Mint I have problems. I want to run this py file at startup as root but I don’t know how to do it. The main question is how to do it.

This is my attempt to solve the problem. It does not work.

I added a file pr.service to folder /etc/systemd/system/:

JavaScript

I created a file pr.sh in folder /usr/local/bin/:

JavaScript

I used these commands:

JavaScript

If I run my program with command

JavaScript

I can see this error with command

JavaScript

I have an error command subprocess.CalledProcess Error: Command ‘wmctrl -l’ returned non-zero exit status 1.

I can change my py file, for example I can run

JavaScript

I can change my py file to see error:

JavaScript

RuntimeError: command ‘/usr/bin/wmctrl -l -p’ return with error (code 1): b’Cannot open display.

I have read about this attempt to find solution: https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux

This is an article how to autorun script in Linux as root. I did these things.

My main goal is to autostart my program as root:

JavaScript

You don’t need to find mistake in my solution. It will be interesting to find any solution.

Advertisement

Answer

I have find solution myself. The key is to use two commands:

JavaScript

allow root opening X windows. And

JavaScript

allow root to read our settings.

So we can rewrite our program.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement