I want to OpenCV executable file run on start-up.

I want to OpenCV executable file run on start-up.

I explored and tried many solutions, but all failed.
Solutions :

  • Launching my script from /etc/rc.local
  • Launching my script as a Linux service
  • Launching my script with crontab -e

I wrote the following in script file.

#!/bin/bash

cd /home/ubuntu/TEST/Camera/build
./cv_test

My script is wrong? If it is not wrong, how do I do?

If you want the file to run upon GUI login (and autologin can be set), then the answer will be different than if you want to replace the GUI login with a particular program. The least invasive will be for the program to run upon GUI login. Can you give more detail on how you want this to run versus what kind of login you expect will need to be manual versus automatic?

NOTE: Prior code may have failed because you didn’t give it a DISPLAY environment variable first…which requires a login first…and the script to run from the same user as the login.

This is my OpenCV code.

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main()
{
cv::Mat img,img_gray;

cv::VideoCapture input(0);

for(;;)
{
if (!input.read(img))
    break;

cv::cvtColor(img, img_gray, CV_RGB2GRAY);

cv::imshow("img", img);
cv::imshow("gray", img_gray);
char c = cv::waitKey(30);

if (c == 27)
    break;
}

}

Then I make a executable file : ‘cv_test’
I want to ‘cv_test’ run at start-up or reboot.
So I made script file in /etc/init.d.

$ sudo -s
$ cd /etc/init.d
$ vi ex_script

This is my script file. Script filename is ‘ex_script’.

#!/bin/bash

BEGIN INIT INFO

Provides: ex_script

Required-Start:

Required-Stop:

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: ex_script

Description:

END INIT INFO

cd /home/ubuntu/TEST/Camera/build
./cv_test

After make the script file, I gave the following command.

$ chmod 755 ex_script
$ update-rc.d ex_script defaults
$ reboot

But when reboot complete, cv_test doesn’t run.

When I follow this page, the directory is automatically made after reboot.

How do I do?

I am only guessing, but I think you need to be sure the graphical desktop is already up and running. Then the environment variable “DISPLAY” needs to be set, e.g., for operating locally on that computer, “export DISPLAY=:0” and run by the same user as the desktop login runs as (so for example your script may need to run sudo).

To summarize, you cannot use the GPU unless there is an environment set up for the user who runs the program (an actively logged in desktop). Part of this is needing a context for the GPU (the login), the other part is security (a user is only allowed to execute a program on a context they own…running CUDA or GPU type code from one user while the GPU is owned by someone else is not allowed…consider using sudo). The DISPLAY environment variable tells the program which login session to associate with (a way to get a context).

Consider autologin as a particular user, and having that login be the trigger to running the program. Then, if you need this to run without actually having a monitor, consider a virtual desktop (which pretends to have a monitor but is purely in software…this allows a context which is associated with a user, thus DISPLAY can be set). For an example of virtual desktops see:
[url]http://ubuntuhandbook.org/index.php/2016/07/remote-access-ubuntu-16-04/[/url]