cd (Change Directory) in a sh script

Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Broadcom SoC based Hardware
Author Message
aampudia
DD-WRT Novice


Joined: 31 May 2011
Posts: 5

PostPosted: Tue May 31, 2011 22:39    Post subject: cd (Change Directory) in a sh script Reply with quote
hi everyone!! i'm sorry if this has been answered in other thread, but i couldn't find any post answering my questions! i've been using dd-wrt for about 5 years now, but i'm a linux/unix noob.

Here's what i got:
- Linksys wrt54g v2 router, with firmware v24 13064 Vintage Std noKaid USB
- USB Mod (http://woodhousellc.com/?p=30)
- Slim Samba 2
- "Optware, the right way" (i need python)
- CoreUtils (installed using ipkg-opt, i need NOHUP)
- 2GB USB stick, with 1 ext3 partition, mounted on /mnt

Here's my problem:
i want a "Wii Media Server" Python script called "Pelis a la Carta" to run 24/7/365 (http://blog.tvalacarta.info/descargas/pelisalacarta-wiimc-3.0.1-python.zip)
so i unzipped it to "/mnt/data/palc" , and if i use telnet and write

-------------------------------------
cd /mnt/data/palc
nohup python pelisalacarta.py &
-------------------------------------

it works flawlessly, but i have to start it up after each reboot. so i wrote a "Startup Command"

-------------------------------------------
/bin/sh /mnt/data/palc/palc.sh > /mnt/data/palc/palcout.txt
-------------------------------------------
(the output is only to have a way to check it it was ran or not, can be deleted)

also, it starts using more and more memory when i use my wii to browse movies. so i wrote a cron job to kill it every 20 minutes

-------------------------------------------
/20 * * * * root /bin/sh /mnt/data/palc/palc.sh > /mnt/data/palc/palcout.txt
-------------------------------------------
(the output is only to have a way to check it it was ran or not, can be deleted)

Both, startup command and cron job, start an SH script (palc.sh) that contains the following

--------------------------------------------
#!/bin/sh
killall python pelisalacarta.py
cd /mnt/data/palc
nohup python pelisalacarta.py &
--------------------------------------------

the problem is that both run the script, but the script does nothing, if i run the script manually using telnet, i get an error saying this

--------------------------------------------
/mnt/data/palc/palc.sh: cd: line 3: can't cd to /mnt/data/palc
--------------------------------------------

i've tried many different scripts and the problem is always the path-working directory or something, and i can't CD to it or define a PATH or whatever.
i've also tried doing the CD in the startup

everything would work just fine if i could CD within the script, and other linux forums say it's possible.. what am i missing here?? is there anything i shouldn't have installed but did? or should have installed but didn't?? please heeelp!!
Sponsor
oxygenx
DD-WRT Guru


Joined: 11 Nov 2007
Posts: 566

PostPosted: Wed Jun 01, 2011 7:12    Post subject: Reply with quote
your usb drive is not mounted at start, 2nd problem might be that the path to python in unkown. use the full path.
_________________
Router: WNDR3300 (wl0: n-Only 5Ghz, WPA2-AES, wl1: g-Only, WPA-Mixed-Mixed)
WDS Node 1: WNDR3300 (wl0: n-Only 5Ghz, WPA2-AES, WDS-connected Router, wl1: g-Only WPA-Mixed-Mixed)
WDS Node 2: WRT54GL (g-Only, WPA-Mixed-Mixed WDS-connected to Router)
Modem: Cisco EPC3202
clients: Notebook 1, D-Link 323, PS3 Slim, Kathrein UFC960 connected to WDS Node 1 via Gigabit Switch. Notebook 2, Deskjet 6980 connected to WDS Node 2
zoomlink
DD-WRT User


Joined: 08 May 2011
Posts: 221

PostPosted: Wed Jun 01, 2011 13:03    Post subject: Reply with quote
Where do you have Optware installed.

Typically your partitions for optware should look something like this (just a recommendation for a 2GB USB stick):
Code:

1 sda1  256MB Ext3   /opt
2 sda2  128MB Swp    /swap
3 sda3  1.6GB Ext3   /mnt



Try it this way:
Make sure your /mnt is mounted and has all the right permissions and that your scripts are marked as executable. (chmod +x scriptname.sh)

Code:

#!/bin/sh
killall python pelisalacarta.py
sh /mnt/data/palc/nohup python pelisalacarta.py &
Jmeier
DD-WRT User


Joined: 27 Oct 2010
Posts: 219

PostPosted: Wed Jun 01, 2011 16:17    Post subject: Reply with quote
That says to me that the dir doesn't exist... Or doesn't exist at the time of execution.

You could put a sleep command in and see if it helps.

sleep xx

xx=seconds. Try 30 or 60 and adjust down from there.

#!/bin/sh
sleep 60
killall python pelisalacarta.py
cd /mnt/data/palc
nohup python pelisalacarta.py &
aampudia
DD-WRT Novice


Joined: 31 May 2011
Posts: 5

PostPosted: Sun Jun 05, 2011 2:52    Post subject: Reply with quote
thanks you guys for the answers, but i still can't make it work...

the USB is mounted everytime at start with automount option enabled, i had already tried using "sleep 60" at the startup command and at the shell script itself but it doesn't help.

i tried using the full path for nohup, python and the python script (media server). like this

---------------------------------------------
#!/bin/sh
killall python pelisalacarta.py
/opt/bin/nohup /opt/bin/python /mnt/data/palc/pelisalacarta.py &
---------------------------------------------

and it kinda works, the thing is that it starts pelisalacarta.py, but that py file starts a bunch of other py files, and those files can't be found and throw errors, cuz the "working directory" is still tmp. i guess the python script uses "absolute path" and not "relative path", and i don't want to modify the media server scripts and all the updates they release.

i have optware installed directly on the "root" of the USB drive, one ext3 partition has a "data", "lost+found", and "opt" folders in it. i tried using the 3 partitions, but didn't know how to mount the first partition to opt, and the third to data. so it was all messed up, nothing worked, not samba, not anything..

the shell script was already chmodded to 777, it shows "-rwxrwxrwx"

i also tried the code Zoomlink gave me, it gives a syntax error, so i deleted the "SH" from the code since its a python script and not a shell script that im trying to run from there. it doesn't work either.

any other ideas?? its something about the CD command, because thats the only thing that doesn't work, and i can't CD to any other directory, not /mnt or /opt or /tmp...
zoomlink
DD-WRT User


Joined: 08 May 2011
Posts: 221

PostPosted: Sun Jun 05, 2011 22:59    Post subject: Reply with quote
Hmm.. can you pls post the output for:

Code:

cat /opt/etc/automount


If you look in Frater's OTRW thread you will find what you need to do in order to always mount your partitions in the same order.

Since you already shoudl have an /opt/etc/automount file, it may just be a function of changing that file and restarting your router so it automagically picks up the mount points the way you want them and memorializes them for all future reboots and/or changes to automount.

Another thought, since you have Optware installed, have you tried using #!/bin/bash for your script?

Unfortnately, I am not too familiar with Python. But I am at a loss on why cd does not work in your script.
aampudia
DD-WRT Novice


Joined: 31 May 2011
Posts: 5

PostPosted: Mon Jun 06, 2011 2:58    Post subject: Reply with quote
nothing... it outputs nothing! i checked and i didn't have that file.. so i googled and found this code

Code:
wget -O /opt/etc/init.d/S35automount http://wd.mirmana.com/S35automount
service automount umount /tmp/c
service automount umount /mnt
service automount start


posted http://www.dd-wrt.com/phpBB2/viewtopic.php?p=602239 by frater

at first it couldn't create the file, so i created an empty file manually, and re-ran the code, and when i used "cat" to check what was in it, i found this
Code:
/opt c771d3c0-3d31-43eb-bc37-9de2c54436fd ext3


i used "service automount" to check it's status, and it said that /opt was in automount but /mnt wasn't so i edited the automount file, copy-pasted that single line into a new second line, but changed "/opt" to "/mnt", now if i check the automount status it says /mnt is being mount with automount
then i tried to run my shell script... same error!! =( if i run it with BASH instead of SH it says "no such file or directory"
zoomlink
DD-WRT User


Joined: 08 May 2011
Posts: 221

PostPosted: Mon Jun 06, 2011 4:47    Post subject: Reply with quote
Something is not right here..

Please re-install OTRW from scratch.

Code:

wget -O /tmp/prep_optware http://wd.mirmana.com/prep_optware

cd /tmp

chmod +x prep_optware

./prep_optware




You never posted the output from fdisk -l.
aampudia
DD-WRT Novice


Joined: 31 May 2011
Posts: 5

PostPosted: Mon Jun 13, 2011 20:30    Post subject: Reply with quote
well.. it works now!!! after reflashing and resetting the router, formatting the USB stick and reinstalling OTRW a few times i came to the conclusion that definetly it wasn't OTRW issue, but the script itself.

so i installed OTRW, Slim Samba2 and PYTHON. i "uploaded" the server script, and started playing with the script again. then i thought "why don't i see what the script that install OTRW does???", so i did, started stripping it down, and after deleting some huge chunks of code, and some iterations, it ended up like this
Code:
#!/bin/sh
PATH=/opt/bin:/opt/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/mnt/data/palc;
killall python pelisalacarta.py
cd /mnt/data/palc;
nohup python pelisalacarta.py & > /mnt/data/palc/nohup.out


note that at the end of PATH y added the folder containing the media server script, note the colon at the end of the CD line as well. and also, im telling it where to put the nohup output file, so i can delete it later with another line of the script, so it will be cleaning its own mess. if i start the script manually it throws some "warnings"/errors, but it works!!

after that, the SH script could be started by the startup command or the cron job from my first post, and both would (kill and re)start the media server so its always always running!!!

then the problem was that the router only has 16MB of ram, so after a little bit of "movie browsing" it was suuuuper slow, fixed it by adding and configuring a SWAP file in the same USB stick! now everything works like a champ!!

now i can sit, turn on the wii and have more than 2000 movies and hundreds of series right there on my TV.. anytime, without doing anything else but choosing what to watch.... think of netflix, but free, and with 100 times more satisfaction!!
thank you, everybody!!
zoomlink
DD-WRT User


Joined: 08 May 2011
Posts: 221

PostPosted: Mon Jun 13, 2011 23:52    Post subject: Reply with quote
Glad to see it worked.

I am curious... what media server is this that you are running and are those movies and tv series in your private collection or you get them from an online storage.

Would love to do similar setup for my Wii or PS3.
aampudia
DD-WRT Novice


Joined: 31 May 2011
Posts: 5

PostPosted: Tue Jun 14, 2011 6:25    Post subject: Reply with quote
it's a script written by a spanish guy (and some others) called "Pelis a la carta" Pelis is short for Peliculas (movies in spanish), so it's "movies a la carte", you can find everything about it on http://blog.tvalacarta.info/plugin-xbmc/pelisalacarta/

it has different versions for different platforms, it works as a plugin on XBMC (Xbox), plex, boxee, and as a server for the wii, i dont know of there's anything for the ps3.

to install it on your router you need to download the "python" version from the "Wii" section of this page http://blog.tvalacarta.info/plugin-xbmc/pelisalacarta/descargar/
(direct link to the actual version file http://blog.tvalacarta.info/descargas/pelisalacarta-wiimc-3.0.1-python.zip )

IF (only IF) youre going low on space on your router you can delete all the images from all the folders within that zip, also delete all the subfolders in the folder "platform" named after platforms you wont use, except for "developer" (for wii only leave "developer" and "wiimc" ). and then upload it to your router. none of all those files are needed at all!

its a python script, so you need to have python installed (sorry for stating the obvious)

to make it work with the wii you need to have " homebrew channel" on the wii, and an app called "WiiMC" (Wii Media Center)

WiiMC has an "online media" part, where you can find streamin radio stations and some movies, but honestly i didn't find any good "channel" for movies and tv, only music.

WiiMC runs from a SD card, in the SD card, along with the application, theres a file called "online media.xml" or something like that, edit it adding a new link to "pelis a la carta" using the LAN IP of your router running the script and the port configured in the file "resources/settings.conf" of the server files.
you can also check the port used if you run the script using telnet without "nohup" (just "python pelisalacarta.py") it will say something like "servidor iniciado en : IP:PORT" (server started at : IP:PORT)

example of the line to add to "online media.xml" :
Code:
<link name=”Pelis a la carta” addr=”http://192.168.1.1:8080″ type=”playlist”>


use the WiiMC version 1.1.7, the latest version, 1.1.8, throws an error using "pelis a la carta"

now put the SD card on the wii, start WiiMC, select Pelis a la carta from Online Media, and go to "Canales", spanish for "channels". there youll find several "channels" which are compilations of media found on different sites, the one i like the most is a channel called "CUEVANA" (media from the Argentinian site "www.cuevana.tv", which is awesome, by the way), you can find that channel under both, "Peliculas" and "Series"

right now cuevana has more than 2000 movies, and more are added every week, and a whole lot of series, updated regularly (episodes are usually uploaded the very same day of the TV release in the US)

what the server really does is grab the links to files hosted on megaupload, hotfile, etc. from those movies sites, and send them to the wiiMC, just the "playlist". WiiMC downloads it from megaupload and plays it.

unfortunately most of all this is in spanish, google translator is your friend now!! or you can ask me, im mexican, i can help you too!! cuevana movies are in the original language, some other channels are in spanish

i hope everything is clear enough as i wrote it, if it's not just tell me and i'll try to be clearer!! xD
zoomlink
DD-WRT User


Joined: 08 May 2011
Posts: 221

PostPosted: Tue Jun 14, 2011 16:29    Post subject: Reply with quote
Awesome.. thanks! Will give it a try when I get some time.
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Broadcom SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum