Linux start script file path issue

Discussion in 'Systems Administration' started by Lonestar78, Jul 8, 2021.

  1. Hello, I am experiencing some weird file path behavior. Let me first say I don't work in Linux often, hardly ever. I am moving some dev/test servers to Linux for just that, testing. I have it all setup and I am able to start it, but not in the way I would think it would work. Let me explain.

    I have created a user called sysadmin and created a home dir from the root it is /home/sysadmin and when I login with the sysadmin user I am in the sysadmin folder by default. I have created a folder inside of sysadmin called servers and I have put my servers inside there, one at the moment for testing. So the full path to the paper.jar file is /home/sysadmin/servers/build01

    So know you know how the file structure is setup let me explain the weird behavior or what I think is weird behavior. from inside the sysadmin home folder if I do ls I see the server folder. So one would think to start the start.sh script you should just be able to do /servers/build01/.start.sh and it would run the start script but instead I get /servers/build01/.start.sh: No such file or directory when the file and the directory do in fact exist. So if I do
    /home/sysadmin/servers/build01/.start.sh the full path even though I am already in the sysadmin folder I get this error. Unable to access jarfile paper.jar but the start.sh script is in the same folder as the paper.jar file. But if I do a cd /home/sysadmin/servers/build01 and then do ./.start.sh then it starts no problem. So they only way it will start is if I run the start.sh while inside the actual build01 folder where the script is. I cannot run it outside from the sysadmin root even if I put the absolute file path to it and that script is in the same folder as the paper.jar.

    I would like to be able to run this from the sysadmin root folder. Is this not possible? Also one would think this would work since if you setup a service to start the server on reboot or startup it uses the same absolute path too, but it doesn't run either. Even if I run it as root like this...

    Code (Text):

    [email protected]:~# /home/sysadmin/servers/build01/.start.sh
    Error: Unable to access jarfile paper.jar
     
    Now I understand I am outside the folder, but I am providing the absolute path to the script and the script is call the paper.jar file in the same folder as the script. What am I missing here? Is this just something as simple as a permission issue? I wouldn't think so since not even root can run it and you have to be in the same dir to run the start script. Is that normal?

    -Thanks!
     
  2. You can check here how I've done it with the shell scripts I use on Ubuntu and macOS.
    https://github.com/mrfdev/1MB/tree/master/Resources/Server

    (start sh is for loading minecraft sh and forking it, and minecraft sh is what goes through the paces to start the server)

    To start a shell script in linux you need to make it a- executable and b- run it lik ./file.sh
    chmod a+x yourfile.sh
    ./yourfile.sh

    if you include a path, and run it outside its directory you need to give it a working dir. you can update your .sh script to do so.
     
  3. I have a working shell script and it works fine and its executable and starts the server IF I am in the same directory as the script.

    The issue is I have to be inside the dir to execute it, I cannot start it from the sysadmin folder root. Which this creates another problem because if I create a systemctl service so it starts on restart. It won't recognize the absolute path either.

    Even if I move the start.sh to that root and then put the absolute path to the paper.jar in the .sh script I get the same error. It almost acts as like Linux doesn't recognize absolute paths. I remember having a problem like that before but don't recall what I did to fix it.

    Can you elaborate on this further?
     
  4. You can update the top of your shell script to first go into the directory the server is. So it can find the files.
    And you can catch if the file is found. Before executing things randomly.