![]() |
#1 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
shell script to randomize files in directory
Hi,
I've tried "sort -R" solutions, and other solutions that use $RANDOM which doesn't appear to be available in the busybox version of /bin/sh or /bin/bash on kindle PW2. Anyone know a way to loop through random (png) files with a bash shell script on a jailbroken Kindle PW2? Thanks! Justin |
![]() |
![]() |
![]() |
#2 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
Here was my script that worked on my Mac, but not the kindle:
#!/bin/sh FILES=(*.png) while true do n=${#FILES[@]} f="${FILES[RANDOM % n]}" while [ "$f" = "$last" ] do f="${FILES[RANDOM % n]}" done last=$f echo "WALLPAPER: $f" sleep 2 done |
![]() |
![]() |
Advert | |
|
![]() |
#3 | |
Going Viral
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
|
Quote:
The Kindle uses Busybox-ash. (Hint: Ash does not do arrays, but Bash does.) So run Busybox-ash on your Mac when testing scripts for the Kindles. |
|
![]() |
![]() |
![]() |
#4 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
Thanks knc! That will help for the future for sure.
So I gave up and learned enough python (I've done PERL for 15 years now) to do it. !/usr/bin/python import time; import glob; import random; import subprocess; last = ""; while 1 == 1: file = random.choice(glob.glob('*.png')); while file == last: file = random.choice(glob.glob('*.png')); last = file; #print file; subprocess.call(['eips','-f','-g',file]); time.sleep(60*60); # switch backgrounds every hour (btw, what's the way to show code on this forum?) Last edited by Eradicatore; 04-04-2014 at 06:43 PM. Reason: code formatting bad |
![]() |
![]() |
![]() |
#5 |
Going Viral
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
|
Code:
!/usr/bin/python import time; import glob; import random; import subprocess; last = ""; while 1 == 1: file = random.choice(glob.glob('*.png')); while file == last: file = random.choice(glob.glob('*.png')); last = file; #print file; subprocess.call(['eips','-f','-g',file]); time.sleep(60*60); # switch backgrounds every hour wrap with either 'code' or 'php' tags. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
BLAM!
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 13,506
Karma: 26047190
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
|
Yup, see the shuffle logic of the ScreenSavers hack for other crazy ideas to deal with this issue, but, yep, it was fun, and I'm bundling a coreutils sort to ease the pain
![]() |
![]() |
![]() |
![]() |
#7 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
So now the next step is of course to have this python script run somehow either automatically or when I do some event. I'm checking if the "eventHandler" hack will help me just call this python script in the background on a suspend event.
Just FYI, I posted that question here. |
![]() |
![]() |
![]() |
#8 | |
Going Viral
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
|
Quote:
|
|
![]() |
![]() |
![]() |
#9 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
Hi knc,
So I installed KUAL, and have it working (like I can toggle USBnet hack etc). Great advice on using that to run my scripts. I also installed the KUAL helper scripts and used that as an example for running my own shell scripts. Seemed like it was going to be a cakewalk. But then I got a synxtax error on the menu.json. I tried using template 1 from this KUAL help link also but that didn't work either. So my issue is I see in the KUAL main menu "KUAL <big bold dot> 1 <submenu arrow>" as the first item. Then if I click in there I see a button second to last with the label "<big bold dot> Syntax quotes/menu.json" I made a sub folder in extensions called "quotes" and in there I have menu.json, config.xml, and bin folder (with my sh scripts). So basically a copy of "helper". My new menu.json looks like this: Code:
{ "items": [ { "name": "QuoteRotation", "priority": 0, "items": [ { "if": "\"KindleTouch\" -m \"KindlePaperWhite\" -m \"KindlePaperWhite2\" -m || ||", "name": "Start", "priority": 1, "action": "bin/wrap_ssloop.sh" }, { "name": "Exit", "priority": 2, "action": "bin/rm_ssloop_lock.sh", } ] } ] } Any ideas? Are there logs somewhere from KUAL that will give more details on the syntax error perhaps? |
![]() |
![]() |
![]() |
#10 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
Wow, that's embarrassing. I forgot to remove the extra comma. :-)
|
![]() |
![]() |
![]() |
#11 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
Ok, so I got KUAL setup to start it, everything is looking great, if I set the timeout between screens at something small to test (8 seconds), it looks to work perfect. Then, I set it to 45 minutes and leave it. Nothing. I figure, ok, maybe the python script died for some reason. Nope. SSH in and ps -ef and there it is.
So time to add some logging. Just figured I would document it here. Does Kindle go into ultra low sleep and so my "sleep" call in python never finishes? |
![]() |
![]() |
![]() |
#12 |
BLAM!
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 13,506
Karma: 26047190
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
|
@Eradicatore: Yup. cf. https://www.mobileread.com/forums/sho...d.php?t=236104
|
![]() |
![]() |
![]() |
#13 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
@NiLuJe, thanks for the link (again). I finally realized I had been reinventing the wheel here, but that was probably good anyway since I got to learn a lot about my way around your community here and all the great things you're working on.
Also, mine is really just a dumbed down version of what he's got, but also I don't need to use WiFi so it may last a bit longer. I'm planning on just preloading a bunch of quotes and maybe updating once in a while after I gather more I want and create them in Gimp. I see the function I need from his work. The wait_for() is perfect. Just setup the RTC of the system and trigger a wake that way. Brilliant!!! I'm used to working on raspberry pi where there is no RTC. :-) Anyway, thanks again. I really really appreciate your helping out a NOOB to this Kindle hacking stuff. |
![]() |
![]() |
![]() |
#14 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
So I have the following python script, with the RTC wake alarm ported over from the bash shell that Peterson did for his onlinescreensaver tool. Again, I just want to make a simplified all in one python script that I can launch from KUAL. I have it make sure the powerd state is screenSaver to start with, and then stop if the powerd state is ever active again.
So I've verified lipc-wait-event is working for other events like goingToScreenSaver. I've confirmed that the rtc0 seems to be working, and that the wake events in my testing are 60 seconds in the future when compared to "since_epoch". And if I sit there with usb attached (so Kindle never goes to deep sleep) then the -s part of lipc-wait-event works to trigger my updates every minute. I'm doing every minute just as a test. So without deep sleep, all is working. Great Then comes the issue. I tried then to exit the ssh and remove the usb cable and it stopped. So then I turned off usb networking and used KUAL to start the script. Again, only one update and then died. At first I assumed something about the RTC was just not working, to send "resuming" events to powerd. But then I noticed that if I logged back in with ssh I could NOT see my python script process anymore. It is running a forever loop so it should be. What I don't get is nothing about Peterson's script seems to do anything differently to keep his while 1 loop going. The only difference I can tell is that one is a bash script and one is python, but that seems like it would be a don't care. Any thoughts on how to prevent kindle deep sleep from apparently killing my background process? Code:
#!/usr/bin/python import time; import glob; import random; import subprocess; def mainLoop (): fo = open("/mnt/us/extensions/quoteRotation/bin/log.txt", "a") fo.write("\n=== starting new rotation ===\n"); # first make sure the screen saver is showing state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']); while state.find("screenSaver"): subprocess.call(['lipc-set-prop', 'com.lab126.powerd', 'powerButton', '1']); time.sleep(1); state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']); last = ""; while 1 == 1: state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']); if state.find("screenSaver"): # maybe rm .lock file here? break; file = random.choice(glob.glob('/mnt/us/extensions/quoteRotation/bin/png/*.png')); # should add check for empty list of png's here while file == last: file = random.choice(glob.glob('/mnt/us/extensions/quoteRotation/bin/png/*.png')); last = file; tstring = time.strftime("%H:%M:%S") fo.write ("{}: screensaver path: {}\n".format(tstring,file)); subprocess.call(['eips','-f','-g',file]); sleepForMin(1); print "got back to main loop" def getCurrTimeInSec (): "This function returns current time in seconds" return int(subprocess.check_output(['date','+%s']).rstrip()) def sleepForMin (mins): "this function will use the RTC to wake back up after so many minutes" endTime = str(getCurrTimeInSec() + (mins * 60)); print "mins:", mins print "currtime:", str(getCurrTimeInSec()) print "endtime:", endTime # disable/reset current alarm subprocess.call("echo 0 > /sys/class/rtc/rtc0/wakealarm", shell=True) # set new alarm subprocess.call("echo {} > /sys/class/rtc/rtc0/wakealarm".format(endTime), shell=True) alarmContents = (subprocess.check_output(['cat','/sys/class/rtc/rtc0/wakealarm']).rstrip()) #print "alarmContents:", alarmContents #print " endTime:", endTime if (endTime == alarmContents): print "they were equal" while getCurrTimeInSec() < int(endTime): remainingWait = int(endTime) - getCurrTimeInSec() if (remainingWait > 0): # wait for device to suspend or to resume - this covers the sleep period during which the # time counting does not work reliably print "about to lipc wait!!! remainingWait:", remainingWait subprocess.check_output("lipc-wait-event -s {} com.lab126.powerd resuming || true".format(str(remainingWait)), shell=True) else: print "failed compare" # not sure whether this is required #subprocess.check_output("lipc-set-prop com.lab126.powerd -i deferSuspend 1", shell=True) return mainLoop() |
![]() |
![]() |
![]() |
#15 |
i void warranties
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
|
Investigating this thread right now:
https://www.mobileread.com/forums/sho...ight=low+power To start learning about the /etc/upstart configurations. |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
invoking sql in a shell script | Mingyar | Kobo Developer's Corner | 17 | 05-31-2013 06:50 PM |
pdf to txt shell script | Mr.Castro0o | 0 | 03-27-2013 07:39 AM | |
Shell script to wait for user input from K3g keyboard | jmseight | Kindle Developer's Corner | 33 | 04-01-2012 04:32 PM |
A way to execute files without shell access?!? | scotsman | iRex | 8 | 03-13-2009 07:35 AM |
Script to convert a directory full of files? | mdibella | Kindle Formats | 2 | 10-14-2008 01:21 PM |