Shell script not running as crontab, but runs manually - java

I have a script to start my Minecraft server on a Raspberry Pi 4. It has to run in a GNU screen because of my backup utility. I have already tried specifying a path and it shows up in the log as a process but it doesn't run.
I would really appreciate some help on this because my search history is so full trying to find the issue but nothing works. The Bash script is as follows:
#! /bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
cd /home/pi/Desktop/Servers/PvP
screen -S pvp java -Xmx2560M -Xms2560M -jar server.jar nogui
My crontab is as follows. I have a new line below this entry so its not that
00 03 * * * ./start.sh
I've tried specifying the full path and that doesn't work either
Edit:
I also created restart.sh which simply calls stop.sh and the calls start.sh
stop.sh is very simple
killall screen
This then calls start.sh. The entire restart.sh script is also very simple
./stop.sh
./start.sh
I put this in my crontab and started a screen session. Low and behold, at the time I set the job to start, screen terminates. I then type screen -ls, expecting to see the newly created window created by start.sh but it has not run. Its beyond me why this is not working. Everything works if done manually, please help this is DOING MY HEAD IN! AAAAAAH!

Specify the actual full path, which must start with a slash. For example, 0 3 * * * /home/username/project/start.sh

Related

Crontab not running a Processing script

I have a python script that I am running from the command line that does three things
1.) Kills all Processing programs currently running
2.) Runs a new Processing program
3.) Shut downs Raspberry Pi
When running this command from the command line, it works flawlessly. Yet, when calling this Python script using crontab, only the 1st and 3rd processes run correctly. What I want to know is why the 2nd command (running a new Processing program) works when I run the Python script from the command line, but not from a crontab?
Here is my Python script
import os # Use operating system
import subprocess # Use subprocess calls
from time import sleep # Use sleep
from subprocess import call
os.system('sudo killall java')
sleep(5)
child = subprocess.Popen('sudo processing-java --sketch=/home/pi/Desktop/LaserCannonProcessing/LCshutdown --run', shell=True) #
sleep(15)
call("sudo poweroff", shell = True)
and here is my crontab
50 20 * * * sudo /usr/bin/python3 /home/pi/Desktop/Shutdown.py
Does anyone know why crontab can not successfully run the command to run a processing program? If so, is there any way I can fix this and make crontab run that line? Thanks
The cron daemon automatically sets several environment variables. The default path is set to PATH=/usr/bin:/bin.
So if the processing-java command is not present in the cron specified path, you should either use the absolute path to the command or change the cron $PATH variable.
Using shell=True is masking the problem...
E.g.
In [7]: child = subprocess.Popen('bla', shell=True)
/bin/sh: bla: command not found
In [8]: child
Out[8]: <subprocess.Popen at 0x107ac8c50>
You can add some debugging to your script to find out the real issue:
try-except around the subprocess call and shell=True
print the os.environ["PATH"]
check permissions on files (if your process needs to read/write to files)

restart console on Minecraft Server

Please excuse me for my inexperience, I am very new to this.
I set up a server on Digital Ocean for Minecraft earlier today. I ran the command to create the world with java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar nogui, and created a screen.
It gave me access to the console where I could type like /op and other commands. Later on, I shut down, and now when I try to rejoin, I'm not sure how to get back to that console. I have more than 1 person on the server, who is on the console right now - I don't know if that should cause it any issue. When I retry to open the console with the command java -jar minecraft_server.jar It says there is already a server running (which there is) but I just want to get back to the console.
Thank you in advance.
If this is a linux vserver or root server, you can use the "screen" tool to access the minecraft console again.
Create a new startup script (start.sh) in the server directory to launch the JAR:
#!/bin/sh
screen -d -m -S "minecraft_screen" java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar nogui
Open your terminal and execute the following in the directory:
chmod +x start.sh
Run your start up script:
./start.sh
To access the minecraft screen use the command "screen -r".
To leave the minecraft screen press Ctrl + A + D
As a player logged into the server, stop the server by typing /stop. This will stop and close the server. Now proceed back to your hosting service and start the console the same way you did the first time. If it worked this way the first time, i can imagine itll work the second time.
just noting in case this becomes a resource for other new server owners, as of writing this comment - screen has been depreciated and tmux is it's replacement
use 'tmux' to create a screen
use 'ctrl+b' then 'd' to detach
use 'tmux attach' to rejoin a screen
i spent a little while searching the internet for this, so i thought sharing it here would be logical - apologies if this question is too outdated to still be relevant

Java console running in a screen autostart on Ubuntu

[Ubuntu 14.04] I have a java console application, and I need to run all time (1st problem), as well as I can re-access the console whenever I want (2nd problem).
To solve the second problem, I use a screen, then run the jar file in it. So that I can re-attach the screen to access my console app.
I am now stuck with the 1st issue. I want to make the screen autostart with OS. Because I need the app running all time. Anyone give me an idea? I appreciate all your help. Thanks.
UPDATE 11/17/2015:
With #janos's help, it works fine except using #reboot. I tried crontab starting the script each 5 minutes and it worked fine. But when i replaced it with #reboot, it did not work anymore!
Finally I chose this solution: Create a screen with a particular name, then create a script sh file to check if the screen with that name existed or not. If not, then run the screen along with java file. Last, create a crontab to run the script each 1h.
Many thanks to #janos for your effort and help.
To run a program after system boot, use #reboot in your crontab:
#reboot /path/to/executable args
For more details, see this Ubuntu help page:
https://help.ubuntu.com/community/CronHowto
As per your comments, you seem to be having difficulty running Java + screen with cron. To help you debug, I suggest to create a custom configuration file for screen, let's call it ~/screen-debug, with a content like this:
screen -t home
screen -t java bash -c 'java -jar ...; echo Press enter to exit; read'
And use a crontab line like this for testing:
*/5 * * * * screen -c ~/screen-debug -d -m -R java
What's happening here:
Run the job every 5 minutes
Use a specific screen configuration
Start screen in detached mode
Reuse the screen session named "java"
If a session with this name doesn't exist, it will create it
If a session with this name doesn't exist, it will reuse it (not start another screen)
There will be two windows in the screen session:
Labeled "home": a simple shell, as if you run screen in your home directory
Labeled "java": the Java program, hopefully happily running. If not happily running, you should see the error message that should help you debug the problem, and a prompt to "Press enter to exit". When you press enter, the shell will terminate in this window.
Once you get this working, then you can replace */5 * * * * with #reboot.

shell script runs when executed manually but executes half way through crontab

I have a java service running on a solaris server. I need to kill this service and restart it every night at a specified time. Hence i have set a cron job to do the same. My script works fine when i execute it manually through command line. But when i set it as a cron job, it executes only half way i.e it only kills the process but does not start it. Kindly assist me. Below are the details:
Restart script:
#!/bin/sh
pkill -u peri java 2>> /dev/null
sleep 3
cd /opt/home/peri/utils/jsb
. /opt/home/peri/utils/jsb/pjsb.new
sleep 3
cd /opt/jar
MonitorExt.sh & > /dev/null
Here pkill is killing the java process. The script pjsb.new is the script which is used to start the java process. Also one more script MonitorExt.sh is used to start another java process.
Any help is highly appreaciated!!!!
Thanks in advance
1) under user 'root', check for some cron error messages in /var/cron/log
2) usually when commands/scripts are running fine manually but not in the cron job, it is because some environment variables are not set in the cronjob context.
So you should make sure that all the necessary environment variables which are automatically set in your default shell ($HOME, $JAVA, ...) are actually set when running in a cron job
I usually call a profile script inside the script or in the cronjob line:
15 17 * * * . $HOME/.profile && $HOME/script.sh
3) You should also prefer full paths for all your scripts and commands:
/usr/bin/pkill
/path/MonitorExt.sh
...

Start Minecraft_Server.jar via PHP Exec() or similar function

I'm trying to build a script that starts minecraft_server.jar (location: user directory / mineserver / minecraft_server.jar ).
I have PHP and Apache installed, and I'm trying trying to start the server JAR from /var/www/html/interface.php.
Via console, the server starts fine by running:
java -Xmx1024M -Xms1024M -jar mineserver/minecraft_server.jar nogui
... from the user directory. So in the interface.php file I have the following (note location listed above):
system('java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui', $retval);
But the server never starts after I visit the file. What am I doing wrong?
Thanks for any and all clues.
I want to say thank you for your help on Thanksgiving. While I haven't found the solution yet, I appreciate the efforts. Thanks again.
Have you checked the error log? It's at /var/log/httpd by default. If anything went wrong, it should be there. Otherwise, it could be starting wrong and I would recommend launching it in a screen so you can hop in and check on anything it's doing.
Install screen through whatever method your distribution uses, and then change the launch string to be:
screen -dmS "minecraft" java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui
The command will be executed with the permissions, and the environment of whatever user account the web server runs under.
In particular, it may have a different PATH entry, and the java command might not even be found, or it might be found somewhere else etc...
Also, be aware of what current working directory it is inheriting from your PHP script.
See exec() and check the output to help yourself do some basic debugging.
Also, you probably want something more like
exec('nohup java etc... > /dev/null 2>&1 &');
so that the process gets properly backgrounded and disconnected from the webserver parent process.

Categories