LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2005, 01:34 AM   #1
warnetgm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 30

Rep: Reputation: 15
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
 
Old 08-17-2005, 02:45 AM   #2
volvogga
Member
 
Registered: Aug 2004
Location: MI, USA
Distribution: Fedora Core 3
Posts: 137

Rep: Reputation: 15
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.
 
Old 08-17-2005, 03:31 AM   #3
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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
 
Old 08-17-2005, 05:48 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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
 
Old 08-17-2005, 06:06 AM   #5
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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.
 
Old 08-17-2005, 07:02 AM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
enemorales: you're welcome.

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

--Jonas
 
Old 08-17-2005, 07:42 AM   #7
warnetgm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 08-17-2005, 05:16 PM   #8
volvogga
Member
 
Registered: Aug 2004
Location: MI, USA
Distribution: Fedora Core 3
Posts: 137

Rep: Reputation: 15
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.
 
Old 08-18-2005, 09:00 PM   #9
warnetgm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 08-19-2005, 12:27 AM   #10
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
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.
 
Old 08-19-2005, 09:33 AM   #11
warnetgm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 30

Original Poster
Rep: Reputation: 15
Thank You Electro,

Anyway how to change permission so it can be executed ?
 
Old 08-19-2005, 09:57 AM   #12
JimBass
Senior Member
 
Registered: Oct 2003
Location: New York City
Distribution: Debian Sid 2.6.32
Posts: 2,100

Rep: Reputation: 49
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
 
Old 08-20-2005, 04:32 AM   #13
warnetgm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 08-20-2005, 05:03 AM   #14
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
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.
 
Old 08-21-2005, 02:34 AM   #15
warnetgm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 30

Original Poster
Rep: Reputation: 15
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 ^^
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu How to run Python script on Login? warnetgm Linux - General 14 03-13-2012 07:11 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 12:43 PM
python script LinuxLala Programming 14 04-07-2004 07:19 AM
/.bash_profile setup to run java program mohapi Fedora 10 01-22-2004 08:43 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 10:34 AM


All times are GMT -5. The time now is 04:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration