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/)

warnetgm 08-17-2005 12:34 AM

how to Run python script on .bash_profile
 
Hi,

I want to run my python script :
/home/user/client.py
on every user login

Since .bash_profile being called on every login (or am I wrong? on every linux start ?) I want to run my script on it.

An expert suggest that I should add just the path of my script to .bash_login

and if .bash_login doesn't exists I must create it first.

I've done that all but not working :(

please help, step by step, I'm newbie in linux

Thank You

volvogga 08-17-2005 01:45 AM

I did a google search on your problem and ran across your previous thread. I was a bit confused. Are you having trouble running the python program as part of the login script, or are you having trouble running the python program in general?

If it is the latter, I know that I have to type 'python /home/user/my_program.py' in order to run the code. As this is not necessary in Windows, it took me a while to figure that out.

If I am way off base, sorry about the post.

enemorales 08-17-2005 02:31 AM

Do you mean when you login or when any user logs in?

In the first case you have to put "python /home/user/client.py" in your .bashrc file. In the second case, I think (but can be wrong) there is a /etc/profile file that is excecuted for every user.

HTH

jonaskoelker 08-17-2005 04:48 AM

enemorales: yep, there's a global profile; /etc/profile on my box.

IIRC, you're a bit wrong regarding .bashrc;
.bash_profile is sourced (i.e. run) for all your login-shells (fx. the C-M-F[1-6] ones)
.bashrc is sourced for all interactive shells (fx. in an xterm)

OP:
if it's for every user:
Code:

# echo "/path/to/script.py" >> /etc/profile
you may (or may not) also want to see the variable $USER.

If it's only for yourself:
Code:

$ echo "python /path/to/script.py" >> ~/.bash_profile
hth --Jonas

enemorales 08-17-2005 05:06 AM

Code:

IIRC, you're a bit wrong regarding .bashrc;
.bash_profile is sourced (i.e. run) for all your login-shells (fx. the C-M-F[1-6] ones)
.bashrc is sourced for all interactive shells (fx. in an xterm)

I'm probably too used to xterm these days. Thanks for the remark, jonas.

jonaskoelker 08-17-2005 06:02 AM

enemorales: you're welcome.

as an exercise, open up an xterm, say "exec login", login. Your profile should have been sourced.

--Jonas

warnetgm 08-17-2005 06:42 AM

I'm creating a python script, usually it work nicely if I do this in console :
#python
>>import client
>>client.init()

But now I want the script I've created to run automatically each time any user login.

Inside .bash_profile what should I write ?
just : python /home/user/client.py ??
does it know how to call the => client.init() function ?
Thank You

volvogga 08-17-2005 04:16 PM

If the commands run fine in the python shell (the >>> prompt), then I should think that they would run in the exact same manner when executed from a python script file. I can't think of any reason why the system would treat the commands any different in this case.

I would just insert the command 'python /home/user/client.py' into the file that you choose to try like jonaskoelker suggested and see if it works.

warnetgm 08-18-2005 08:00 PM

Halo Experts,

I can run :

python /home/user/client.py

at terminal.

But it doesn't run automatically when I'm insert the command into one of this :
.bash_login
.bash_profile
/etc/profile

I tried to insert at begining or end of the bash script, but still not run my program (I've test it by logoff and login)
Except for something weird, if I insert the command at the beginning of .bashrc file then the the script automatically run when I open the Terminal.

What I need is the script is run on every user login automatically.

Thank You

Electro 08-18-2005 11:27 PM

At the very beginning of the python file add

#!/bin/python

This path may not be right so change it to the full path to python but keep #!.

Then change the permissions so that it is executable.

warnetgm 08-19-2005 08:33 AM

Thank You Electro,

Anyway how to change permission so it can be executed ?

JimBass 08-19-2005 08:57 AM

Code:

chmod a+x filename
will allow everyone on a system to execute the program. You may need to be root if the location where the script is is not somewhere where you have control of.

Peace,
JimBass

warnetgm 08-20-2005 03:32 AM

Electro, I've already put

#! /usr/bin/python2.4

and also JimBass, I've already

chmod a+x /home/user/client.py
chmod a+x /home/user/client.pyc

But after all still doesn't run my script. I've tried put

python /home/user/client.py

at : .bash_login and etc/profile

still not work after I've test by log off and log in again

But the strange is it working when I'm RESTART (LogOut-Restart in Ubuntu)
Does in ubuntu is different ?

What things should I do now ? plese Help

Thank You

Electro 08-20-2005 04:03 AM

The line

#! /usr/bin/python2.4

is wrong. It should be

#!/usr/bin/python2.4


You can try use

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

to debug the script.


Take off the .py and run the script after you export the path. Your python script might be failing.

warnetgm 08-21-2005 01:34 AM

Hi Electro,
Already fix it to #!/usr/bin/python2.4
and tried again in several .bash_xxx logoff login still not working

I've delete the .pyc before test to know wether it is actually compiled or not, then the result is NO .pyc exists, so I think that seems bash didn't call to compile my script at all.

This is the script looks like is there anything wrong with it ?

#!/usr/bin/python2.4

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()

and when I tried to run it on terminal :
# python
Python 2.4.1 (#2, Mar 30 2005, 20:41:35)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import client
>>> client.main()
>>>

its work perfectly no error, so what is the problem really is, I'm really confuse, please Help

Thank You alot for helping me ^^


All times are GMT -5. The time now is 05:47 AM.