Qt: launch a message box periodically with crontab in ubuntu - c++

I am trying to launch a Qt message box every minute. I added a line in crontab to run the Qt program's executable and redirected the error output to a file in my home directory.
There is no problem with the Qt program that launches the message box because I tested it, but when I try to launch it with crontab the following error arises:
QXcbConnection: Could not connect to display
Aborted (core dumped)
I checked that over the internet and found a thread that might be helpful: https://unix.stackexchange.com/questions/148945/could-not-connect-to-display-in-one-user-account/149026#149026
I believe the first response gives the solution but it's not clear.
It suggests to use x11 to share the desktop and xauth to add security measures but doesn't specify how to configure them. I have xauth already installed but the error persists.
Any ideas about how to solve this problem?

I think the DISPLAY environment is missing. I suggest you wrap your application in a wrapper script (let's call it horloge.sh):
#!/bin/sh
DISPLAY=:0
export DISPLAY
/home/salwa/computing/cpp/horloge
Then put the horloge.sh in your crontab. Don't forget to do a chmod u+x horloge.sh so that the script is executable.

Make sure that the user that launches your app in the crontab line is you. Otherwise, it may not have permissions to use the X server.

Related

Google cloud compute startup script ignored with no logging

I have a standard Debian 8.9 instance on google cloud compute (GCE) where my startup script is ignored.
In the custom metadata field, for startup-script, I am trying to run an Rscript (which is used for batch execution of R files), followed by a system shutdown, with the following:
#! /bin/bash
sudo /usr/bin/Rscript /home/myuser/launch_script.R
sudo shutdown -h now
Starting the instance is immediately followed by a shutdown and the Rscript is ignored. Removing the last line to shutdown causes the GCE instance to start, but the Rscript to be ignored. Running just "sudo /usr/bin/Rscript /home/myuser/launch_script.R" from the terminal results in the script being run. It has a chmod of 755, so I don't think this is a permissions issue.
In addition to this problem, I have read elsewhere that logging should happen in /var/log/, but there is nothing there. Instead, I have a bunch of log files (that only contain the start-up script and nothing else) in the root of my instance:
I got in touch with Google cloud support, who gave the following response:
script definition is kept under /var/run/google.startup.script
If the script does not run initially, you can force it manually with : $ sudo google_metadata_script_runner --script-type startup # for Debian, or # sudo /usr/share/google/run-startup-scripts # on Ubuntu and older images
I'm posting this information here, because it is not in their documentation (as of August 2017). I'm not sure how helpful it is, since the google.startup.script didn't exist in my case (using the latest Debian image on GCE), but I did run the other commands.
However, I think my main issues were:
I was using autossh to connect to a remote database. The startup-script was running before autossh. Building a 40 second delay into the script and running the script as a user (not sudo-type root) seems to have solved this problem for now. Autossh was being run as the main user, which I think gets loaded before lower-privilege user-defined scripts get loaded.
I was using some gcloud commands from the user account which had its own authentication issues. Running gcloud auth login as the user and ensuring correct permissions on my private key solved this.
Always remember to check the messages and syslog files in /var/log for troubleshooting. This allowed me to see the order of things being loaded at system-boot.

Run my executable automatically as root ubuntu

I am working in Ubuntu 14.04 I have an C++ application which I have compiled. I get the executable which name is "program". Well the thing is that this program use opencv and sockets then if I try to execute it in the terminal like this:
./program
I got this error:
listener: socket: Operation not permitted
setsockopt: Bad file descriptor
This error appear because I using socket and I need to run my application as root, so I tyoe this:
sudo ./program
This request my password and the program works propertly.
The thing is that I want to run this "program" automatically when I start my Ubuntu, but it does not work. I have another application the name is "camera" which only use opencv, no socket, then I do not need to run as root. So I put this application in the startup application of ubuntu and the "camera" application start at the begining without problem.
So I guess that my problem is that I need to run as root my "program", but I can not get it.
Someone could help me?
Many Thanks
You need to change the ownership of the program to root:root and use the setuid permission so that it runs with the permissions of its owner, not the user who invoked it.
sudo chown root:root ./program
sudo chmod a+s ./program
Now the program will run with root privileges for all users.
Hope that helps.
Side note: As a programmer you should strive to ensure that programs that run with root privileges are free of security holes, so that they're not vulnerable to buffer overflow exploits, etc.

Popen xauth list

I am trying to run a wxpython script via VNC. When i try to open it i get the error:
X11 connection rejected because of wrong authentication.
Unable to access the X Display, is $DISPLAY set properly?
I solve it via:
xauth list
sudo xauth add raspberrypi/unix:10 MIT-MAGIC-COOKIE-1
and then the corresponding cookie. Thing is i wanted to automate this in the init of the script, so that the user would not need to do it every time. i have written the following script:
import subprocess
import string
p=subprocess.Popen(['xauth','list'],stdout=subprocess.PIPE)
(out,err)=p.communicate()
out1=out.split('\n')
for line in out1:
t=line.split()
y=subprocess.Popen(['sudo','xauth','add',t[0],t[1],t[2]],stdout=subprocess.PIPE)
which works fine but i am still not able to get the program to run. Checking the output i realize that i am not getting the same output if i run xauth list from shell and if i run it with Popen.
Any help would be much appreciated!

How to make Cygwin the default shell for Jenkins?

I'm trying to come up with some sensible solution for a build written using SCons, which relies on quite a lot of applications to be accessible in a Unix-like way, using Unix-like paths etc. However, when I'm trying to use SCons plugin, or Git plugin in Jenkins, it tries to invoke the plugins using something like cmd /c git.exe - and this will certainly fail, because Git was installed using Cygwin and is only known in Cygwin shell, but not in CMD. But even if I could make git and the rest available to cmd.exe, other problems arise: the Cygwin version of Git expects paths to have forward slashes and treats backward slashes as escape characters. Idiotic Windows file-system related issues kick in too (I can't give Jenkins permissions to delete my own files!).
So, is there a way to somehow make Jenkins only use Cygwin shell, and never cmd.exe? Or should I be prepared to run some Linux in a VM to have this handled?
You could configure Jenkins to execute the cygwin command with the specific shell command, as follows:
c:\cygwin\bin\mintty --hold always --exec /cygdrive/c/path/to/bash/script.sh
Where script.sh will execute all the commands needed for the Jenkins execution.
Just for the record here's what I ended up doing:
Added a user SYSTEM to Cygwin, mkpasswd -u SYSTEM
Edited /etc/passwd by adding the newly created user's home directory to the record. Now it looks something like the below:
SYSTEM:*:18:544:,S-1-5-18:/home/SYSTEM:
Copied my own user's configuration settings such as .netrc, .ssh and so on into the SYSTEM home. Then, from Windows Explorer, through an array of popups I've claimed ownership of all of these files to SYSTEM user. One by one! I love Microsoft!
In Jenkins I now run a wrapper for my build that sets some other environment variables etc. by calling c:\cygwin\bin\bash --login -i /path/to/script/script
Gave it up because of other difficulties in configuration and made Jenkins service run under my user rather then SYSTEM. Here's a blog post on how to do it: http://antagonisticpleiotropy.blogspot.co.il/2012/08/running-jenkins-in-windows-with-regular.html but, basically, you need to open Windows services, then find Jenkins service, select it's properties, go to "Logon" tab and change the user to the "this user".
One way to do this is to start your "execute shell" build steps with
#!c:\cygwin\bin\bash --login
The trick is of course that it resets your current directory so you need to
cd `cygpath $WORKSPACE`
to get back to the workspace.
Adding to thon56's good answer: this is helpful: "set -ex"
#!c:\cygwin\bin\bash --login
cd `cygpath $WORKSPACE`
set -ex
Details:
-e to exit on error. This is important if you want your jobs to fail on error.
-x to echo command to the screen, if desired.
You can also use #!c:\cygwin\bin\bash --login -ex, but that echos a lot of login steps that you most likely don't care to see.

Building project from cron task

When I build project from terminal by using 'xcodebuild' command I succeed, but when I try to do run same script from cron task I receive error
"Code Sign error: The identity '****' doesn't match any valid certificate/private key pair in the default keychain"
I think problem is in settings and permissions of crontab utility, it seems crontab does not see my keychain
Can anyone provide me terminal command how to make my keychain visible for crontab
I encountered a similar issue with trying to build nightly via cron. The only resolution I found was to create a plist in /Library/LaunchDaemons/ and load it via launchctl. The key necessary is "SessionCreate" otherwise you will quickly run in to problems similar to what was encountered with trying to use cron -- namely that your user login.keychain is not available to the process. "SessionCreate" is similar to "su -l" in that (as far as I understand) it simulates a login and thus default keychains you expect will be available; otherwise, you are stuck with only the System keychain despite the task running as your user.
I found the answers (though not the top answer currently) here useful in troublw shooting this issue: Missing certificates and keys in the keychain while using Jenkins/Hudson as Continuous Integration for iOS and Mac development
You execute your cron job with which account ?
most probably the problem !!
You can add
echo `whoami`
at the beginning of your script to see with which user the script is launched.
Also when a Bash script is launched from cron, it don't use the same environment variable (non login shell) as when you launch it as a user.
When the script launches from cron, it doesn't load your $HOME/.profile (or .bash_profile). Anything you run from cron has to be 100% self-sufficient in terms of it's environment. I'd suggest you make yourself a file called something like "set_build_env.sh" It should contain everything from your .profile that you need to build, such as $PATH, $HOME, $CLASSPATH etc. Then in your build script, load set_build_env.sh using the dot notation or source cmd as ericc said. You should also remove the build-specific lines from your.profile and then source set_build_env from there too so only one place to maintain. Example:
source /home/dmitry/set_build_env.sh #absolute path
. /home/dmitry/set_build_env.sh #dot-space notation same as "source"