0

Beginner in Ubuntu 16.4 . I have two commands which needs to be started when the computer starts up
Command 1: java -jar ~/folder/abc.jar
Command2: ~/folder/q/l32/q -p 8712
Both of these commands workd perfectly fine from teminal.Both from the home directory.
I have created 2 bash files one with

#!/bin/sh
java -jar ~/folder/abc.jar

another with

#!/bin/sh
~/folder/q/l32/q -p 8712
  1. How do I run these bash files individually? Double click doesn't run them.
  2. If I want these two scripts to be run at startup what I do? I modified the rc.local file and added the path of these two bash files but its not working.
  • rc.local runs as root after startup but before login. If your jar requires a display or your-user-related environment variables, it will fail if run before login. In 16.04 and newer, a proper systemd target is preferable to rc.local. – user535733 Mar 24 '17 at 12:46
  • yes my jar would need user variable..what is systemd target?Can you explain how to do it? – yellow boy Mar 24 '17 at 12:49
  • That's an entirely different question, asked and answered here several times already. – user535733 Mar 24 '17 at 12:50
  • Your script uses relative paths. Either cd to the appropriate directory in the script using absolute paths or use absolute paths for all the files mentioned – muru Mar 24 '17 at 12:59
  • 1
    Simply add them to startup applications use absolute paths, like @muru mentioned, and possibly you need a little break, depending on what the commands should do. In that case: /bin/bash -c "sleep 10 && command". In the latter case, you do not need abs path. (Dash > Startup Applications > Add). – Jacob Vlijm Mar 24 '17 at 14:51
1

All users on buntu systems have a special startup script file stored in their home directories that is read every time they log in to the system using a so-called nonlogin or interactive shell. All commands listed there are run as if they were literally typed in at the command prompt. This file is called .bashrc (bash stands for bash shell which is the default shell used on all buntu systems and rc stands for resource configuration). Open that file using the nano text editor like this:

$ nano ~/.bashrc

Move the cursor to the very bottom and place your commands there (not exactly sure what you are using them for):

java -jar abc.jar
q/l32/q -p 8712

Then press ctrl + O to save the file and ctrl + X to exit. Now, the next time you boot up your computer, all these commands are going to be run automatically for you.

Your first question:

How do I run these bash files individually?

You need to make them executable:

$ chmod a+x [FILE]

Then, run them from the command like by just specifying their names. For example, I've got the following script (actually, there are a million ways to run executable files at the command line):

echo "Hello World!"

Now, I need to make it executable:

$ chmod a+x script.sh

And now I can run it:

$ bash ./script.sh
| improve this answer | |
  • The ~/.bashrc file is read every time an interactive bash shell is started - not when a user logs in. – steeldriver Mar 24 '17 at 13:45
  • Yes I tried this...once I start the terminal the script got started.Also my first command runs forever so the second command is not executed at all. So this doesn't work. – yellow boy Mar 24 '17 at 13:51
  • That probably depends on the commands you're trying to run. But what I've described up here is how it's done in principle. – misha Mar 24 '17 at 13:54
  • Try using absolute paths. Something like: java -jar /home/yellowboy/abc.jar (and I'm sure what the other command is and is supposed to do) – misha Mar 24 '17 at 13:59
1

Okay,so finally I did it.Let me explain the steps.

My file didn't have root access./etc/rc.local was trying to run it and was failing.As rc.local runs the file in root context.

Therefore I switched to crontab where I can specify the file to be run with respect to which user

crontab -e -u myusername

Once inside the crobtab file i used

@reboot /home/username/pathToMyBashFile

It worked.

| improve this answer | |
0

Pull out a terminal and edit your bashrc config file.

$ vim ~/.bashrc

place the line below in the bashrc. press esc, press shift+;, enter :wq to save the file.

exec /path/to/file.sh && path/to/path/file2.sh

| improve this answer | |
  • 1
    come back to to give us response. – saviour123 Mar 24 '17 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.