Change directory and execute file in one command

Hi, On boot I need the RPI to execute this command cd blynk-library/linux sudo ./blynk --server=xxx.xxx.xxx.xxx --token=xxxxxxxxxxxxxx
With the format above the RPI will not execute because the format will not allow the RPI to navigate those directories. How can I reformat the command where the RPI will connect to my Blynk server on boot?

You shouldn’t need to change directory. Just enter the full path in crontab.

Hi, Costas

It does not work. I tried your earlier recommendation as well.

` #!/bin/sh

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 9443
java -jar /home/pi/server.jar -dataFolder /home/pi/Blynk &
cd ~/blynk-library/linux
sudo ./blynk --server=xxx.xxx.xxx.xxx --token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx &`

I have tried various ways and it will never auto start on boot.

Seems to me it can not navigate directories with that command or it navigates and I don’t have the correct format to tell it to follow through with the rest of the command. I have also tried inserting &, &&, $ in the command and nothing is working. If I type the following at pi@raspi:~ $ prompt

`cd blynk-library/linux sudo ./blynk --server=xxx.xxx.xxx.xxx --token=xxxxxxxxxxxxxxxxx`

It will just change to dir but thats it. Seems to me that’s probably what its doing at boot when the command is grabbed from crontab.

Did you set up your Pi with the regular username of pi?

Do you connect to your Pi if you type the following (with relevant server and token details):

go to your root directory

cd ~

then

sudo ~/blynk-library/linux/./blynk --server=xxx.xxx.xxx.xxx --token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Yes that puts the juice to her. worked great.
sudo ~/blynk-library/linux/./blynk --server=xxx.xxx.xxx.xxx --token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I tried applying the above to the script but it did not fire on reboot.

You don’t have to reboot the server to test the script.

I guess you have some of your paths mixed up.

Where is the script and are you logging in as pi / root / some other user?

To test the script once you have made it executable and you are in the correct directory it is simply:

./scriptname

Convention is that scripts have a .sh suffix but it’s not required.

You should also be able to run the script with:

sh scriptname

Test your script without reboots until everything runs as it should and then check crontab.
If you are struggling let me know and we can look at your paths.

Ok thanks!

First try with ./

`pi@raspi:~ $ ./blynksetup

-bash: ./blynksetup: Permission denied`

Second try with sh *notice the error but it did start!

`pi@raspi:~ $ sh blynksetup

Error: Unable to access jarfile /home/pi/server.jar
[0]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.2 on Linux

[5001] Connecting to 192.168.1.138:8442
[5010] Ready (ping: 9ms).
`

permission denied suggests you didn’t make the script executable (and will certainly not run in crontab).

In the directory for blynksetup enter:

ls -la

and tell me -drw… sequence for the script.

Then do:

chmod +x blynksetup

and tell me the sequence again (you might need sudo for chmod but try without first).

Then paste your script here.

BEFORE
-rw-r–r-- 1 root root 398 Jan 11 07:52 blynksetup

AFTER
-rwxr-xr-x 1 root root 398 Jan 11 07:52 blynksetup

OK now it is executable. Try ./blynksetup again and you shouldn’t get permission denied.

You still have some path errors though.

Paste the script.

Ok, I did edit a path in the script I forgot about. I corrected that and I ran the file, I get this.

`pi@raspi:~ $ ./blynksetup

Error: Unable to access jarfile /home/pi/server.jar
chmod: cannot access ‘blynksetup’: No such file or directory`
[0]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.2 on Linux

No modification made ./blynksetup: line 12: @reboot: command not found pi@raspi:~ $

My Script (rather your script):

`#!/bin/bash

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 9443
java -jar /home/pi/server.jar -dataFolder /home/pi/Blynk &
cd ~/blynk-library/linux
sudo ./blynk --server=192.168.1.138 --token=xxxxxxxxxxxxxxxxxx &

sudo chmod +x blynksetup

crontab -e

@reboot sh blynksetup &`

Wow.

The chmod is a one off requirement that is not needed in your script, remove that line.

Is crontab -e and @reboot… also in your script as they don’t go in the script either?
If they are in the script remove them and I will explain crontab when the script works correctly.

change

cd 
sudo ./blynk --server=192.168.1.138 --token=xxxxxxxxxxxxxxxxxx &

to

sudo ~/blynk-library/linux./blynk --server=192.168.1.138 --token=xxxxxxxxxxxxxxxxxx &

We still need to find the path to your Blynk server but again we will come back to this.

Paste new script and response you get.

This is what I get now

pi@raspi:~ $ ./blynksetup pi@raspi:~ $ Error: Unable to access jarfile /home/pi/server.jar sudo: /home/pi/blynk-library/linux./blynk: command not found

Here is script

`#!/bin/bash

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 9443 java -jar /home/pi/server.jar -dataFolder /home/pi/Blynk & sudo ~/blynk-library/linux./blynk --server=192.168.1.138 --token=xxxxxxxxxxxxxxxxxxxxxxx &

I made the mistake of including in the script.
`sudo chmod +x blynksetup

crontab -e

@reboot sh blynksetup &`

sorry

1 Like

Sorry I missed a backslash in the last line, it should be:

sudo ~/blynk-library/linux/./blynk --server=192.168.1.138 --token=xxxxxxxxxxxxxxxxxxxxxxx &

run again and confirm you just get 1 error regarding the jar file.

Yes
pi@raspi:~ $ Error: Unable to access jarfile /home/pi/server.jar

is all

Progress.

Try this without the script (note the ~):

java -jar ~/home/pi/server.jar -dataFolder /home/pi/Blynk &

Edit: just wondering where your server is.

Try these commands (not in a script):

cd ~/home/pi
ls

do you see server.jar?

Replaced java line. Getting this now.

`Error: Unable to access jarfile /home/pi/home/pi/server.jar`

Appears to be in my home dir

`pi@raspi:~ $ ls

Blynk Desktop oldconffiles projects server.crt superscript
blynk-library Documents Pictures Public server.key Templates
blynksetup Downloads PIGPIO python_games server.pem Videos
certbot-auto logs pigpio_dht22 Scratch start wiringPi
certbot.log Music pigpio.zip server-0.21.1.jar static`