Cannot run script/Java program on Raspberry Pi 3B boot - java

I'm simply trying to make a program run when the Raspberry boots up. The idea being it runs, so there is no need for a mouse/keyboard to manually start the program.
I've read forum after forum on how to run a script on boot on a Raspberry Pi. I feel like I've exhausted every method.
I have tried:
scheduling a cronjob
sudo crontab -e and then added the code
#reboot /file/path/myscript/
Putting my progam in the /etc/init.d/ folder and updating it in the boot sequence.
file moved to /etc/init.d/myscript/
make executable sudo chmod 755 /etc/init.d/myscript/
register script sudo update-rc.d defaults
Adding it to the .bashrc folder in the /pi directory
navigated to /home/pi/.bashrc/
added ./myscript/ to the file.
None of these have worked for me, whereas for everyone else on those forums, it seems to have solve their problem. Is there something I'm missing from any of these methods?

Related

sudo: effective uid is not 0 when running under tomcat service

I have a small java web app (grails), deployed under tomcat 8, from which I would like to execute a script on the local server using sudo. On a regular debian/ubuntu server all I have to do is use visudo to allow the tomcat user to execute sudo without a password on that particular script, and everything works as expected. When I tried installing the same war file on the raspberry pi (model 3b+, raspbian 10 - buster), booting from an SD card, the execution of the script always fails with the error "sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges'.
In an effort to track down this issue I have written another small executable jar that performs the same function (i.e. attempts to launch the script using sudo). This test program works as expected when logged in interactively (bash) as both the 'pi' and 'tomcat8' users (I had to set a shell for the tomcat8 user to order to get an interactive login). I then used strace to try and diagnose the issue. All I could glean from that is that getuid() is returning 111 (tomcat8) when trying to launch sudo when running under the tomcat8 service, but will return 0 when running in bash.
I have also written a small c program that simply calls getuid() and prints the result. If I run it under the tomcat8 user interactively (i.e. sudo su tomcat8), it prints '111' when I run it without sudo, and '0' when I run it with sudo. When I try and launch this program from the web-app (using process builder) I get '111' when the command is run without sudo, but I get the 'effective uid is not 0 ...' error when the command is prefixed with sudo.
I have checked mount, and there are a number of mounts with the 'nosuid' attribute, but not the root '/' directory where /usr/bin is located, and /usr/bin/sudo looks to have the correct permissions:
pi#raspberrypi:~/dev $ ls -l /usr/bin/sudo
-rwsr-xr-x 1 root root 147560 Jan 13 2019 /usr/bin/sudo
In desperation I have tried a couple of other things to just see what effect they might have:
* Added the tomcat8 user to adm, sudo and other groups
* Attempted to remount the other mounts without the nosuid attribute, although I couldn't remount about 6 or so because the mounts where in use.
Neither of these appeared to have any effect.
So it seems to me that the tomcat8 user can use sudo when in bash, but not when running as a daemon. Can anyone give me some ideas as to what is going on here? Is there anyway to diagnose or trace how an effective uid is determined by the os?
Other things that may be significant:
I installed both openjdk-8-jdk and tomcat8 via apt, and even though raspbian uses systemd, tomcat8 is launched via an init.d script. Not sure if this is causing uid issues.
If it is the SD card having some mounts with the nosuid attribute causing the problem, why doesn't it fail when running interactively?
It turns out it was an issue with the way the daemon is started, probably due to changes in the way Debian 10 starts daemon processes. I removed the tomcat init.d script, and replaced it with a systemd unit file, and included the following properties:
[Service]
...
NoNewPrivileges=false
AmbientCapabilities=CAP_SETGID CAP_SETUID
SecureBits=keep-caps
This allows the daemon to actually call setUid(0) successfully.

Launching java as a service and specifying the user

As an introduction, let's just say I'm a real noob using linux. I try to do the things right, don't hit me if it's ugly.
So, the problematic. I'm trying to run some jars as webservices on an ubuntu server. I created a specific user (nuxservice) with no pwd. I edited sudoers to enable a few users (myself & root) to sudo using this account with no password.
I then took a lot of inspiration from : http://www.jcgonzalez.com/linux-java-service-wrapper-example
Most is working, only one real problem, my java process seems to not have the right to create its logging files.
I run my services with a classic
sudo service myservice start/stop/restart
The command line that are launching my services are :
nohup sudo -u nuxservice java -jar myjar.jar myargs
When I do a ps -ef, the services are launches with my nuxservice user.
When I do ls -ld, nuxservice is the owner and have the rights.
If I launch the command in a terminal myself, it works. When launching as a service, my logs files are not created.
Any clues mates ?
So...
It was kinda silly.
My user rights were fine. Problem is, I did not set the working folder in my script so Java was all lost considering the creation of the folder/files for logging.
All I had to do was adding a little
cd $PATH_TO_JAR
And it was all set !

Can't execute java program on Raspberry Pi on startup

I use a Raspberry Pi 3 Model B with Raspbian Jessie.
I have a java program that I want to compile and run on start-up. I figured I need to launch it through /etc/rc.local. I have a command to compile the java-files and one command to run them.
sudo javac -cp
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/.jar
/home/pi/Desktop/MultiSensor_v2.0_Java/.java
sudo java
-Djava.library.path="/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/lib"
-cp "/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/opencv-320.jar:/home/pi/Desktop/MultiSensor_v2.0_Java/"
Main &
When I run these commands from the terminal it works perfectly. I tried to put these commands in the /etc/rc.local file before exit 0. On reboot, the compilation works but the actual program never executes. How do I get my java program to run upon startup using the two commands above?
I had this problem, too. I solved it with the following workaround:
I created a shell-script that runs the java programm. Then I called the shell-script on startup.
I managed to solve it by doing a bash-script as #ILikeCOding said, but I had to change the location from were it was called.
I moved the bash-script to the root directory and edited the autostart script located in:
/home/pi/.config/lxsession/LXDE-pi/autostart
In that file, I added a line to launch my script. I think this works because the autostart-script launches when you log into your user and not on boot, therefore the program can launch properly. Not sure if that's the case, but it works so I'm happy.

Booting Java Application On Startup - Raspberry Pi - Raspbian - Shell Script

I've been attempting to boot a java application via a shell script on a Raspberry Pi 2 startup for a while now with absolutely no success. I've been through countless threads and tutorials with no joy.
I'm running a java application via a shell script.
rc.local
I've tried putting a reference to the shell script in the etc/rc.local file.
I've had the application in the usr/local directory and i've also tried moving it to the home/pi folder. All permissions are set to full on every file.
su - pi -c "bash /home/pi/logon.sh &"
Has no effect on boot, the shell script runs fine when you run it from the terminal.
Chrontab
I've edited the chrontab file. I've tried multiple variants on the end of this file, again nothing worked on boot.
etc/init.d
I've also placed the shell script in this folder and ran the command: sudo update-rc.d /etc/init.d/logon.sh defaults. There are no complaints, it seems to work, shell script works when executed manually, however nothing starts up on boot.
I'm assuming that either Java isn't initiated at the point of boot or the shell script attempts to run before Raspbian boots into its interface since non of the above methods work. I don't mind losing the Raspbian interface if neccessary, in fact this would be preferable. I simply want a java application to start up when the Raspberry Pi boots. Any ideas?
Thanks
i've done it with this wrapper : https://stackoverflow.com/a/21283530/5066919
and you'll see that he use the "nohup" (no hang up) command, like this the app don't close when the user log out
Also becarefull if you use "scanner.hasNext()" command : https://stackoverflow.com/a/33456615/5066919

How to run .jar file on start up in ubuntu?

I have written a javafx media player.I want to run this on start-up. The mediaplayer.jar file is located at Desktop.The player plays the files inside a data folder which is in the same directory.
Thanks Raedwald.
I studied the post you mentioned and I finally resolved it.
Here the steps I did.
startapp.sh
#!/bin/bash
java -jar /home/usr/local/bin/vedioplayer.jar
created the above script and saved it in /etc/init.d
make sure you allow execution of the shell script.
sudo chmod +x /etc/init.d/startapp.sh
After that run the follwing command
sudo update-rc.d startapp.sh defaults 98 02
Also you can add the script to Startup Applications list from Applications.

Categories