How Do I Run Python Scripts At DSL USBStick Bootup


Forum: Apps
Topic: How Do I Run Python Scripts At DSL USBStick Bootup
started by: wasgate

Posted by wasgate on Oct. 19 2004,14:53
Hi folks!

I now have DSL 0.8.2 booting up from a PNY 256MB USB 2.0 USBStick/KeyDrive/PenDrive/ThumbDrive (/dev/sda1), including python.uci.

Now, how do I run one, or more, of my own Python scripts, stored on the USB Stick, automatically, at bootup?

I've located Python in /opt/python/bin, and I suspect that there is some command that will allow me to execute/run Python from any other directory path.

In another post, I found the following, which might be related:

export PYTHONPATH=/opt/python/bin
export PATH="$PATH:$PYTHONPATH"

Am I on the right track?  Where (which file) do I modify with these two lines, or whatever else that is needed?

Also, what file needs to be modified so that my Python script(s) are executed automatically at bootup?

BTW, I wasn't able to 'multi-partition' the PNY 256MB USB Stick into sda1 & sda2 (The PNY FAQ webpage said that it wasn't possible).  I probably was just doing something wrong.  I'll leave it for another day.

Thanks for all the informative posts, past & future!

Wasgate

Posted by jls legalize on Oct. 19 2004,19:58
maybe your scripts could be useful 4 other people?
Posted by ke4nt1 on Oct. 19 2004,20:16
Yep,
What you found will help you get to where you want to be.

e.g.

You want "myfav.py" to run at boottime .

make a script like this..

#!/bin/bash
export PYTHONPATH=/opt/python/bin
export PATH="$PATH:$PYTHONPATH"
python /home/dsl/myfav.py

Save the script under a name like " start_myfav "
Add a line for the start_myfav script to your /opt/bootlocal.sh file .. This will autorun it at boottime
e.g   /home/dsl/start_myfav

You will need to place copies of BOTH myfav.py AND start_myfav
in a path location, like /home/dsl/ , as I did in the scripts ..
If you place them anywhere else, edit your entries accordingly.

Then you need to add both files to your /home/dsl/filetool.lst ,
so your backup picks them up..

home/dsl/myfav.py
home/dsl/start_myfav

Now, create a backup , either thru the dslpanel or the menu.

Then reboot, ... when you reach the boot: prompt
add the line " restore=sda1 "  to your bootup commands ..
( or restore=??  wherever you made your backup write to )

The system will restore the backup, and run the bootlocal.sh ..
The bootlocal.sh will find and execute the start_myfav script ..
The start_myfav will find and execute the myfav.py in a python env.

73
ke4nt

Posted by wasgate on Oct. 20 2004,01:32
Thanks, ke4nt/Kent, for the thorough reply.

I've had some success, but ran into a problem with the backup.tar.gz creation process.

First though, I want to mention that I decided to keep the 'start_myfav' bash script and my Python script(s) on the keydrive (sda1), so that I wouldn't have to modify filetool.sh for their sake.

Next, even though /dev/sda1 is automatically mounted as /cdrom, I added 'mount -t vfat /dev/sda1 /mnt/sda1' to bootlocal.sh.  The next line after this was '/mnt/sda1/MyPython/start_myfav' to execute the 'start_myfav' bash script (/mnt/sda1/MyPython is where I'm keeping the bash script and Python scripts).

The advantage to keeping the bash script 'start_myfav' and my Python script(s) on the sda1 keydrive, in my mind, is so that I won't have to modify filetool.lst on their behalf (bootlocal.sh is already in the list of backed up file, in filetool.lst).

I then attempted to do a backup (to backup the change to 'bootlocal.sh'), but had a bit of trouble figuring out what to type into the filetool 'Device:' input field.  I finally settled on 'sda1' which eventually created 'backup.tar.gz' on the root of sda1.  So far so good.

I then rebooted, and used 'dsl restore=sda1 mydsl=sda1' at the 'boot:' prompt.

Once booted, I verified that bootlocal.sh executed by checking that /dev/sda1 got mounted to /mnt/sda1, which it had.  However, the line '/mnt/sda1/MyPython/start_myfav' apparently didn't get executed, as my Python script didn't start up.

So, I started tweaking this particular line to try to get it to work.  I'd make a change to bootlocal.sh and then do a backup, prior to rebooting.  I did this once, and for some reason decided to manually delete backup.tar.gz on sda1, just prior to doing another backup, after modifying bootlocal.sh.

Then, the problem cropped up.  I'd attempt to do the backup, using either the DSLPanel or DSLMenu, but it wouldn't create backup.tar.gz on sda1.

Since I was starting to get tired, I decided to call it quits and post this progress report post.  I hope it's written well enough to get the idea of the problem across, as I'm still tired!

I'll return to the problem tomorrow, with a fresh mind.  I'll get it figured out.

Thanks again for the detailed info.

Wasgate

Posted by henk1955 on Oct. 20 2004,07:14
if you boot from /dev/sda1.
then /dev/sda1 is mounted as /cdrom.
dot NOT EVER mount /dev/sda1 again to /mnt/sda1
the problem is that at backup time the filetool.sh tries to find the sda1 in the mtab file. it will find 2 entries for /dev/sda1.
therefore the backup will fail.

if you want your python script to run use:
/cdrom/MyPython/start_myfav
and not
/mnt/sda1/MyPython/start_myfav

Posted by wasgate on Oct. 20 2004,13:54
Thanks, henk1955, for the info.  I was wondering whether double mounting sda1 was a bad thing.  Now I know.

So, now that I've got my backup.tar.gz problem sorted out, I've gotten back to figuring out why my Python script isn't being run automatically at USBStick bootup.

start_myfav will execute if I manually run it from a BASH command prompt!

Here are the contents of the 3 applicable files:



bootlocal.sh ==================================
#!/bin/bash
# put other system startup command here
/cdrom/MyPython/start_myfav                      <-- This line isn't executing!!!
===========================================
When I had the 'mount -t vfat /dev/sda1 /mnt/sda1' line in, it was being executed okay.


start_myfav ==================================
#!/bin/bash
export PYTHONPATH=/opt/python/bin
export PATH="$PATH:$PYTHONPATH"
python /cdrom/MyPython/myfav.py
==========================================
This script runs just fine if I manually execute it.


myfav.py ===============================
#!/usr/bin/env python
print '\n\n'
print 'Hello World!!!'
print '\n\n'
========================================
This is the sample/test Python script, FWIW.

The line '/cdrom/MyPython/start_myfav' in bootlocal.sh is NOT being executed, but will execute if I manually execute it at a BASH command prompt!  Here's the output for when I manually execute start_myfav:

==========================
root@box:~# cd /opt
root@box:/opt# more bootlocal.sh
#!/bin/bash
# put other system startup command here
/cdrom/MyPython/start_myfav
root@box:/opt# /cdrom/MyPython/start_myfav



Hello World!!!



===========================================


So, the question is, why won't the above bootlocal.sh execute start_myfav?

Does anyone see the problem?

Thanks very much.

Wasgate

Posted by ke4nt1 on Oct. 20 2004,14:48
Make sure the file start_myfav is executable by everyone..
as root user in shell  > " chmod 777 start_myfav "

Also, try changing the line in your bootlocal.sh from

/cdrom/MyPython/start_myfav

to

exec /cdrom/MyPython/start_myfav

Let us know..

73
ke4nt

Posted by roberts on Oct. 20 2004,17:18
First a comment. It has been posted many times that it is not a good idea to use the flash device as a real hard drive. But when I implemented the ability to save backups on the same partition of the flash, then this opens up the capability for someone to use the flash device in a hybrid way such as this approach. However, this has nothing to do with your "problem". Also Python has nothing to do with the "problem".

I believe the script is working. The issues are several. It has to do with the location of the executing script and the supporting environment at that time.

Where were you expecting the output "Hello World" to appear? If you boot your system with dsl 2 restore=sda1 you will see your script output. But a normal boot, boots directly into X. If you exit the window manager you should see your output. Your script would need a controlling rxvt window to display in. But then you have another issue, as X is not started to even have an rxvt window at the time bootlocal.sh is running.

Now, if your Python scripts are to do only "root" non-X tasks then your approach is OK, my comment still applies.

One thing to keep in mind is the various "autoexec.bat" locations in DSL.

System command(s) (root), implies non X use /opt/bootlocal.sh
User commands non X use /home/dsl/.bash_profile
User commands X based use /home/dsl/.xinitrc

The final answer for you, the Python programmer, lies in which type of command/program you will be writing.

You have the wrapper concept working fine. Now it is the location depending on the program's operating envirnoment requirements.

Posted by wasgate on Oct. 20 2004,23:32
Eureka!!!  I finally got it to work.

First thing:  roberts' comments about USB Stick usage made me rethink, so I moved my bash and python scripts from the USB Stick (sda1, aka /cdrom) to /home/dsl, at least for the time being.  Thanks, roberts!

Second thing:  ke4nt's comments about permissions and ownership caused me to watch/fix the ownership/permissions closely.  Thanks, ke4nt/Kent!

I must admit that I'm still a bit confused with permissions, groups and ownership, because I've noticed that moving/copying files around, as well as editing files, will change these, on occasion.  I need to do more reading on the subject, and get a handle on it.

The 'Bottom Line' is that /opt/bootlocal.sh was:
-rw-rw-r--    1 dsl      staff          74 Oct 20 13:23 bootlocal.sh

When I changed it to:
-rwx------    1 dsl      staff          74 Oct 20 13:23 bootlocal.sh

The whole process worked!


I don't know if/how bootlocal.sh's permissions got changed.  I almost never do any chmod cmds, so if it was changed, it wasn't me (intentionally, at least :)  I'll have to reinstall DSL on my USB Stick and then check /opt/bootlocal.sh to find out (unless someone out there can look at theirs and tell us).

Also, the bash script, /home/dsl/start_myfav (that sets the Python path env stuff) needs to be executable.  The Python script, myfav.py, doesn't, however (for the edification of the other newbies out there!).

The interesting thing, which roberts mentioned, was that my python script ran before the windows manager started up (the tux icon was in the top left corner of the screen).  Once my python script finished, it continued on into the desktop environment.  After DSL init'd into runlevel 5, and after /cdrom/backup.tar.gz is restored, the Python script executed.

Also, there's a chance I had the python script running earlier, but just didn't notice it during the boot process (my 'Hello World' python script was awefully quick).  I'm glad I changed my test python script to one that runs longer than the old 'Hello World' script:

Here's what I ended up with, FWIW:

   import time
   print '\n\n'
   print 'Hello World!!!'
   print '\n\n'
   time.sleep(5)

BTW, this is a handy method of putting a delay in the boot/initialization (dmesg?) 'flood' that occurs!!!  Like I said earlier, this Python script was executed just after the init to runlevel 5 and the backup.tar.gz restore from /cdrom (sda1, the USB Stick).

I was really wanting my script to run in a window on the desktop, but I'll save that for some other time.

Thanks very, very much, gang!  You guys definitely know your stuff!

73 es tnx agn
Wasgate

Posted by ke4nt1 on Oct. 21 2004,01:20
Good Work !

If you would like to try to get the program running in a shell on the desktop,
try placing your commands to execute "start_myfav" in your /home/dsl/.xinitrc
file, instead of your /opt/bootlocal.sh..

Look inside the .xinitrc, and you'll see what going on in there.
The only rule is to make sure "fluxbox" is the LAST line in there.

To add an "open shell" command to your "start_myfav" , look in your
/home/dsl/.fluxbox/menu file..
There are many examples of rxvt being opened, and programs executed
with the -e switch. You may want to lengthen your sleep time, as the
shell will close after the script has completed its execution..

One bonus is that your /home/dsl/.xinitrc should already be in your filetool.lst !

73
ke4nt

Posted by wasgate on Oct. 21 2004,15:10
For the sake of clarity and thoroughness, I thought I'd post my "final, first cut" on this topic.

I decided to start fresh, so I reinstalled DSL 0.8.2 on my PNY 256MB USB 2.0 USBStick/KeyDrive/PenDrive/ThumbDrive,
so that I could document the permission/ownership of bootlocal.sh, etc.

I also redid/restructured my Python script bootup files.  I decided to go against robert's advice and I'm keeping my
Python-related script files on the USBStick (sda1, aka /cdrom).  Now that I understand the process, I can always change it back.
After all, this isn't mission-critical, avionics flight software. :)

Here are the applicable modified/created files and their permissions/ownership, which I
maintained vigorously:

-rwxr-xr-x    1 root     root           88 Oct 20 22:48 opt/bootlocal.sh
-rwxr-xr-x    1 root     root           52 Oct 20 22:39 opt/bootlocal.sh_orig
-rwxr-xr-x    1 dsl      staff         189 Oct 20 22:43 home/dsl/filetool.lst
-rwxr-xr-x    1 dsl      staff         140 Oct 20 22:36 home/dsl/filetool.lst_orig
-rwxr-xr-x    1 root     root          125 Oct 20 22:57 cdrom/MyPython/MyPyBootupScript
-rwxr-xr-x    1 root     root          113 Oct 20 23:02 cdrom/MyPython/MyPyBootupScript.py

Here are listings of the files that I created or modified:

=====================================
opt/bootlocal.sh:

#!/bin/bash
# put other system startup command here
/cdrom/MyPython/MyPyBootupScript
=====================================

=====================================
cdrom/MyPython/MyPyBootupScript:

#!/bin/bash
export PYTHONPATH=/opt/python/bin
export PATH="$PATH:$PYTHONPATH"
python /cdrom/MyPython/MyPyBootupScript.py

=====================================

=====================================
cdrom/MyPython/MyPyBootupScript.py:

#!/usr/bin/env python
import time
print '\n\n'
print 'Hello World!!!'
print '\n\n'
time.sleep(5)

=====================================

=====================================
home/dsl/filetool.lst:

#!/bin/bash
# put other system startup command here
opt/ppp
opt/bootlocal.sh
opt/powerdown.sh
home/dsl/filetool.lst
home/dsl/.fluxbox
home/dsl/.xtdesktop
home/dsl/.xinitrc
home/dsl/.xserverrc
home/dsl/filetool.lst_orig
opt/bootlocal.sh_orig

=====================================
Note that I decided to permanently maintain original/unmodified copies of filetool.lst and bootlocal.sh.

You Live, Linux and Learn!

All the best,
Wasgate

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.