1

Here am I again after successfully installing Ubuntu MATE 14.04 with the help of AskUbuntu (thanks a bunch!)

What am I trying to achieve now is the following:

  1. Launch a Terminal with root privileges at startup
  2. Change the working directory to Desktop
  3. Execute Jar file with the "java -jar filename.jar" command

What is the easiest way to do this, preferably without installing any third parties?

EDIT: I am not only looking how to run a script at startup, but also launching a command line and executing a command.

  • 1
    Put the line java -jar /full/path/to/filename.jar before the line exit 0 of /etc/rc.local, it will do the job for you..why would you want to all these when what are you trying to do could be easily achievable.. – heemayl May 11 '15 at 19:57
  • 3
    To close voters: here the problem is mostly related to the difficulty of opening a Terminal at startup and leave it open, so the linked answer doesn't really address the problem – kos May 13 '15 at 15:17

1 Answers1

1

You can spawn a bash root shell inside a mate-terminal instance, change the mate-terminal's working directory to ~/Desktop and execute your jar file inside it at startup by adding this command to Startup Applications:

mate-terminal -e "sudo -H /bin/bash -c \"cd ~/Desktop && java -jar executable.jar; /bin/bash\""
  1. Hit the Super key, type "Startup Applications" and hit Enter
  2. Name you command, type the name in the "Name" field and type the command in the "Command" field
  3. Click on "Save" and click on "Close"

image-1

Command breakdown:

  • mate-terminal -e "<command1>": opens a mate-terminal instance and runs <command> in it
  • sudo -H /bin/bash -c \"<command1.1>\": spawns a bash root shell and runs <command1.1> in it
  • cd ~/Desktop && java -jar executable.jar; /bin/bash: changes the mate-terminal's working directory to ~/Desktop, executes java -jar executable.jar and spawns another bash root shell which will be available for further uses
kos
  • 35,891
  • Very useful info, but what if I have to pass the sudo password as a parameter as well? Would it be gnome-terminal -e "echo password | sudo -S /bin/bash -c "java -jar ~/Desktop/executable.jar; /bin/bash"" ? – KiralyCraft May 12 '15 at 08:16
  • @KiralyCraft This is very ugly, but it should do. Please note that using commands including password in clear text is never safe, and this time is twice as unsafe because you're also saving the password in Startup Application: bash -c 'echo "password" | sudo -SH gnome-terminal -e "bash -c \"java -jar ~/Desktop/executable.jar; bash\""'. I also added the -H option to sudo to set the bash's instance home to root – kos May 12 '15 at 09:10
  • Although it is unsafe which I can do nothing but agree about, I will try this and come back with the results, hoping for the best. – KiralyCraft May 12 '15 at 09:42
  • Got it working with some fine adjustments. Thanks! – KiralyCraft May 12 '15 at 14:27
  • @KiralyCraft Glad that it worked but can you explain which adjustments? The command above worked for me on 15.04 – kos May 12 '15 at 17:25
  • 1
    First, I had to change "gnome-terminal" to "mate-terminal" since i'm on Ubuntu MATE. Second, I had to change the directory to the app's folder, which wasn't done automatically. Third, I had to specify the full path to java (because I might not have configured it correctly, but it works anyway). My final script was: bash -c 'echo "password" | sudo -SH mate-terminal -e "bash -c \"cd /home/kiralycraft/Desktop/KirLoga && /usr/java/jre1.8.0_45/bin/java -jar /home/kiralycraft/Desktop/KirLoga/KirLoga.jar 2; bash\""' – KiralyCraft May 13 '15 at 15:11
  • @KiralyCraft Thanks, I didn't notice you were using Ubuntu MATE, I'm updating the answer accordingly then – kos May 13 '15 at 15:19