Score:0

How to start script as root with DISPLAY=:0

jp flag

I have a script should run with root privileges and also should use display(opencv imshow in it.) How can I run that script at reboot?

I tried with a my.sh file:

export DISPLAY=:0
/usr/bin/python3 /path/to/myscript.py

cronjob:

@reboot /bin/bash /path/to/my.sh

Yet it doesnt start.

Can you please help me?

user10489 avatar
in flag
Check root's mailbox for errors from cron, and maybe add those errors to your question? Assuming you are logged into the console as a non-root user, you may need to use xauth to copy your display cookie to the root account. Or, alternately, run the script in cron as the same user that is logged in.
Hakan Ulusoy avatar
jp flag
I am logging in as root. crontab is also root. my main suspecious is, export DISPLAY. I run that sh as root from ssh, doesnt work either; returns with cannot connnect display. However if I run the sh as user, it works(without root privileges, raise error again)
user10489 avatar
in flag
Are you logged into the graphical console at the same time you use ssh? If so, as what user?
user10489 avatar
in flag
When you log in with ssh, are you expecting the script to run on the display local to you, or the display that is on the machine you are using ssh to access?
Hakan Ulusoy avatar
jp flag
actually it changes. Sometimes login in sometimes not. But user is same, only one user exist tho. Do you mean, you cant use display from ssh or cronjob until user login in and start xsession?
user10489 avatar
in flag
You can't open a window in a session that doesn't exist. If you want to display a window in your local session rather than a (possibly non-existant) session on the remote machine, then you should not be messing with DISPLAY.
Score:1
cn flag
raj

You cannot run a job that is run @reboot with a graphical display because at the moment the system boots, there is no graphical display up and running yet.

You should add that script to "Startup programs" of your desktop session. As for running as root, the most foolproof method of running a script as root for me is to use a binary wrapper that is setuid root.

Write a following short C program (call it wrapper.c for example):

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
   int rc;
   setuid( 0 );
   rc=WEXITSTATUS(system( "/path/to/your/script" ));
   exit(rc);
}

Compile the program using: gcc -o wrapper wrapper.c (you need to have build-essential package installed for that).

Make the wrapper file setuid root: sudo chown root:root wrapper followed by sudo chmod o+s wrapper

Then configure path to your wrapper file into startup programs of your desktop session.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.