LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to Run python script on .bash_profile (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-run-python-script-on-bash_profile-354085/)

jonaskoelker 08-21-2005 05:16 AM

when posting code, *always* put it in code tags.

try running your script as both:
Code:

$ ./client.py
$ /path/to/client.py

There are basically three scripts you need to know about: /etc/profile, ~/.bash_profile and ~/.bashrc;

you probably want to add yours to /etc/profile.

Just added a python script to my /etc/profile, it all acted as it should.

--Jonas

warnetgm 08-22-2005 05:19 AM

I'm trying to do it but still not working after I tried logoff and login again :(

Unfortunately, maybe I'm misplaced something ?

warnetgm 08-22-2005 05:20 AM

Halo Jonaskoelker,

Sorry for the code, now I know the rules, ok this is my /etc/profile

Code:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
else
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games"
fi

if [ "$PS1" ]; then
if [ "$BASH" ]; then
PS1='\u@\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi

export PATH

umask 022

Please modify it so I can simply copy paste it into my /etc/profile, because I tried to put the


Code:

$ ./client.py
$ /path/to/client.py

in there but still not working :( unfortunately, maybe I'm misplaced it within /etc/profile...

Please Help, Thank You :)

jonaskoelker 08-22-2005 08:44 AM

Try running some other python code:
Code:

umask whatever
...

python -c "print 'hello, world'"

Does it work? Does `echo hello, world' work?

You didn't copy
Code:

$ ./client.py
$ /path/to/client.py

verbatim, did you? The $ is part of the prompt, not the command (which I'm sure you already knew).

hmm... *scratches head* --Jonas

Electro 08-22-2005 06:03 PM

There are many ways to run scripts. One way is

. /path/to/client.py

The . means run this script in the same environment as the script that ran it. Do not get confused with ./path/to/client.py because it is different.


The second way is

exec /path/to/client.py
or
/path/to/client.py

This way will run the script in another environment with unknown environment variables. Probably, it will use default environment variables that is set with bash, sh, csh, and other shells.


You may want to use "#!/usr/bin/python2.4 -d -t" in your script to check for tabs and warn if the tabs are inconsistent. Python is dependent on tabs or indents. The script that you posted does not have any indents to tell python which lines of code is part of other codes. I am a Python beginner, but you may want to add error catch commands to make sure it works correctly when it does have problems. The script should have a way to figure out if 192.168.100.100 can be access. Usually during boot, the profile script is run before the network is ever started. You probably want to run the python script as a daemon, so the python script can detect using a while loop if networking is up.

warnetgm 08-22-2005 11:19 PM

Jonas,
This is the code I'm trying :
Code:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
else
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games"
fi

#python /home/support/client.py


if [ "$PS1" ]; then
  if [ "$BASH" ]; then
    PS1='\u@\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
        . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

export PATH

umask 022

python -c "print 'hello, world'"

./client.py
./path/to/client.py

But it doesn't even compile the script since there is no .pyc file. Also the code print hello world, didn't show anything after I've logoff and login.

Electro,
Sorry actually the script have indent just it turn doesn't have indent when I'm paste here maybe because I didn't use *code* quote. This is the real one :
Code:

#!/usr/bin/python2.4 -d -t

from socket import *

def main():
        s = socket(AF_INET, SOCK_DGRAM)
        hostname = '192.168.100.100'
        port = 1973
        s.connect((hostname, port))

        code = chr(203);
        stationid = '30';
        s.send(code + code + stationid + ' Games')

        code = chr(203) + chr(214) + chr(201) + chr(198) + chr(001)
        s.send(code + 'LINUX CLIENT PROGRAM MASUK MEJA ' + stationid);

if __name__ == "__main__":
    main()

I've also add -d -t but nothing happen after I've log off and login again.

You are right that I need the network running first ofcourse that is what I want to achieve by scripting this is to send data to the server program. So what should I do now ? what daemon work different ? anyway I still need to make sure that my program just running after each login and shutdown just when log off, while still running in the background other than that.

Actually before I'm migrate all of my clients into linux, I'm using WinXP, in there I'm create an exact program ( using Delphi) and add my program shortcut to StartUp folder. After that its working like I want it, the program executed after each login, and stay in the background to receive command by server program, and shutdown when the computer logoff/turnoff.

Actually I want to achieve that too in my linux clients, it must be possible, just I'm newbie to linux and so I didn't understand how linux work on something like that, but eventhough I know it will be hard, I have spirit and patience to learn :))

Please Help
Thank You

jonaskoelker 08-23-2005 08:47 AM

Btw, what I said about bashrc being run at interactive clients turn out to be wrong. It say in /usr/share/base-files/dot.bashrc that ~/.bashrc is run on on starting non-login shells.

OP: try `python <absolute path to script>'

hth --Jonas

warnetgm 08-23-2005 11:35 PM

Do you mean absolute path is :

Code:

python /home/user/client.py
or anything else ? because that is already tested

warnetgm 08-25-2005 02:36 AM

halo ? is there anything else I should do ?

jonaskoelker 08-25-2005 03:51 AM

I'm out of ideas. I have *no* clue why this `just works' for me and not for you.

you may want to look at how I did it: http://jonaskoelker.homeunix.org/~jonas/transcript.txt

hth --Jonas

warnetgm 08-30-2005 10:12 AM

Hi Jonas,

Please see this is what I did to my /etc/profile

Code:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
else
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games"
fi

if [ "$PS1" ]; then
  if [ "$BASH" ]; then
    PS1='\u@\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
        . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

export PATH

umask 022

python /home/client/client.py
client@MIS:~$ cat /home/client/client.py
#!/usr/bin/python2.4 -d -t

from socket import *

def main():
        s = socket(AF_INET, SOCK_DGRAM)
        hostname = '192.168.100.100'
        port = 1973
        s.connect((hostname, port))

        code = chr(203);
        stationid = '30';
        s.send(code + code + stationid + ' Games')

        code = chr(203) + chr(214) + chr(201) + chr(198) + chr(001)
        s.send(code + 'LINUX CLIENT PROGRAM MASUK MEJA ' + stationid);

if __name__ == "__main__":
        main()

but after I triend logoff and login again, unfortunately it still doesnt work :(
Please Help

Thank You

warnetgm 08-30-2005 10:30 AM

And the strange is like ussual, it is always working when I Shutdown or Restart computer.

jonaskoelker 08-30-2005 02:00 PM

Hmm...

Try inserting a `print "hello, world"' in the script.

Try giving the absolute path to python (i.e. /usr/bin/python /home/client/client.py)

Echo something right before the line w. `python', to make sure control gets there.

Make sure the file is world-readable.

warnetgm 08-31-2005 12:02 AM

Ok I will try that,

But could you explain, why the script only being executed correctly when I'm Turn Off or Restart ?
Other than that, such as when I'm starting linux or logoff or login, the script never got executed.
Just when Turn Off the script Run.
I think there gotta be something here, the script is OK and run but only not at the moment I've expected.

Do you have any idea regarding that ?

jonaskoelker 08-31-2005 08:00 AM

Quote:

Do you have any idea regarding that ?
No, I'm totally out of ideas.

--Jonas


All times are GMT -5. The time now is 02:00 PM.