LinuxQuestions.org
Review your favorite Linux distribution.
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 10-05-2008, 02:48 AM   #1
K.out
LQ Newbie
 
Registered: Oct 2008
Posts: 23

Rep: Reputation: 1
help writing logon script to access and update a database


I am trying to write a php script to access a database a and write user info into a table upon login. Currently I appended the script to the bottom of the profile file in /etc/profile. However, it doesn't appear to be working when I log in. Below is the script that i have.

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|$1($|" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}

# ksh workaround
if [ -z "$EUID" -a -x /usr/bin/id ]; then
EUID=`id -u`
UID=`id -ru`
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

if [ -x /usr/bin/id ]; then
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi

HOSTNAME=`/bin/hostname`
HISTSIZE=1000

if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
CLASSPATH=/usr/lib/jdk1.6.0_07/lib:.
PATH=$PATH:/usr/lib/jdk1.6.0_07/bin:.


export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC CLASSPATH

for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done

unset i
unset pathmunge
<?
//make connection
$connection = pg_connect("dbname=postgres
user=postgres
host=localhost");

//report status
if(!connection){
print("Connection Failed.");
exit;
}

//insert data into userstats

//insert data into userstats
pg_exec($connection, "INSERT INTO userstats VALUES
('$USER','localhost')");

?>

Can somebody help me find a way to solve this problem?

Last edited by K.out; 10-05-2008 at 02:51 AM.
 
Old 10-05-2008, 03:51 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
First, I'll recommend you place your script in its own file for execution. Then call or source this script from within the correct shell startup script.

/etc/profile, ~/.bash_profile, and ~/.profile is used by login shells
~/.bashrc is used by interactive shells

Login shell startup scripts are for doing things once per login (to the system, not just a new terminal window)
Interactive shell startup scripts are for doing things for every shell that is invoked

The problem may be that your shell is not a login shell, and you certainly don't want your command script running on every shell launch. A login shell will have a - (dash) in front of its command name in ps output.

Place unique echo commands in each of ~/.bashrc and ~/.bash_profile to convince yourself which one(s) is read and sourced.
 
Old 10-06-2008, 02:05 AM   #3
K.out
LQ Newbie
 
Registered: Oct 2008
Posts: 23

Original Poster
Rep: Reputation: 1
Thank you Mr. C. I was able to establish that the script need to be run from /etc/bashrc after using your suggestion of entering the echo commands into the different files. Now I am having trouble getting my script to execute.

Below is my script which I have saved in the /etc directory




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head></head>
<body>
<?php
//make connection
$connection = pg_connect("host=localhost dbname=postgres
user=postgres");

//report status
if(!connection){
print("Connection Failed.");
exit;
}

//insert data into userstats

//insert data into userstats
pg_exec($connection, "INSERT INTO userstats VALUES
('$USER','localhost','now()')");

//close connection
pg_close($connection);

//close connection
pg_close($connection);

?>
</body>
</html>






The error that is printing is :
./loginscript: line 1: syntax error near unexpected token `newline'
./loginscript: line 1: `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">'
 
Old 10-06-2008, 02:52 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
The "script: is an HTML document, not a shell script. Do you expect a browser window to open?

Since the content is not directly executable, instead launch the browser with the document as an argument. Eg:

firefox mydoc.html
 
Old 10-06-2008, 08:12 AM   #5
K.out
LQ Newbie
 
Registered: Oct 2008
Posts: 23

Original Poster
Rep: Reputation: 1
Ok, thanks for letting me know that. My goal is simply to write a script that will update my database on login. I do not really desire to open up the web browser. Do you know how I can revise the "script" to accomplish this task? Thanks for your help.
 
Old 10-06-2008, 12:26 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
You can use any of the following to implement your script:

*) bash - using postgresql command line programs
*) PHP command line, using a php script file
*) perl or python, with their postgresql bindings


Pay attention to their return values.

Your script is very close already to being a PHP script - just remove the HTML code, and place the line :

#!/path/to/your/php

as line 1 in the file. Change the file to executable (chmod a+x /path/to/script) and then run it (/path/to/script).
 
Old 10-09-2008, 10:19 AM   #7
K.out
LQ Newbie
 
Registered: Oct 2008
Posts: 23

Original Poster
Rep: Reputation: 1
Mr. C,

Thanks for the help. I managed to get communication with the database have the script add rows. It was tricky because there where two versions of php installed and I was trying to call the wrong version so it was not recognizing some functions. I am still having trouble though. Currently, I call the script from bashrc. This runs the script each time a new shell is started. However,sometimes it can't run because the script is in the root directory. What I want to due is have the script run and update the database as soon as users login to NX. I tried putting the script in .profile and that did not seem to update the database. Do you think you know how to solve my problem?



Thanks again,
K.out
 
Old 10-09-2008, 11:27 AM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Create your script. If other users are going to use it upon login, install it in /usr/local/bin or /usr/local/sbin.

Then, in each of the /etc/profile variants for all the user shells your users use (how's that for a mouthful of users!), call your /usr/local/{s}bin/script.

Each shell has its set of files that are scanned for existence and sourcing on either creation of a new login shell, or a non-login interactive shell. Check INVOCATION in man bash, and similar locations for other shells you use.
 
Old 10-14-2008, 09:29 PM   #9
K.out
LQ Newbie
 
Registered: Oct 2008
Posts: 23

Original Poster
Rep: Reputation: 1
I placed the script in /usr/local/bin directory and called it from the /etc/profile file. This works for communicating with the database on login; however, it writes 15 entries to the database each time there is a login. Do you know what would cause this? Also, if you do, can you help me fix this problem as well?


Thank you,
K.out
 
Old 10-14-2008, 10:41 PM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Most likely what is happening is the file /etc/profile is being read by each shell started to launch some GUI app that comprises your desktop environment (I'm assuming you're using a GUI). The file /etc/profile is used for login shells, but unfortunately different distributions configure their user startup environments differently, and I don't keep track of which disto does what anymore.

Can you give details on your distribution, version, and GUI or non-GUI environment?
 
Old 10-15-2008, 09:39 AM   #11
K.out
LQ Newbie
 
Registered: Oct 2008
Posts: 23

Original Poster
Rep: Reputation: 1
When I log in I see a fedora 8 desktop. However, when I type env in the command line of the terminal, I see nothing refering to fedora 8. I only see stuff referring to GNOME. This would be version 2.20.3 from the Red Hat Distributor. I am not sure if this helps or makes sense.
 
Old 10-15-2008, 10:21 AM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
See my post #10 here, particularly the pdf file referenced that gives you the correct locations for post-login script running in GDM.

http://www.linuxquestions.org/questi...ghlight=zenity
 
Old 10-15-2008, 01:51 PM   #13
evaluatinglinux
Member
 
Registered: Oct 2008
Posts: 45

Rep: Reputation: 15
In your .profile file append the command - "./runupdate.sh"
and the runupdate.sh should contain a sql insert script that will insert the desired values to the database.


This should do the trick.

Debian Kernel

Last edited by evaluatinglinux; 10-25-2008 at 02:51 AM.
 
Old 10-15-2008, 03:32 PM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
I think the OP wanted a *global* solution:

Quote:
What I want to due is have the script run and update the database as soon as users login to NX.
Also, the OP tried ~/.profile already:

Quote:
I tried putting the script in .profile and that did not seem to update the database.
 
Old 10-17-2008, 12:45 PM   #15
evaluatinglinux
Member
 
Registered: Oct 2008
Posts: 45

Rep: Reputation: 15
Could you try running the script (from my example earlier - execute /runupdate.sh from the command prompt). See if this does an insert into table. If it doesn't then voila ... you have identified the root cause of the issue.
 
  


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
cron job running PHP script to update MySQL - access denied error Beauford2008 Linux - Newbie 9 09-24-2008 07:17 PM
WinXP pro don't run "logon script = logon.bat" Bengt7jqb Linux - Server 2 04-13-2008 12:11 PM
PHP writing to database shmuck Programming 2 03-08-2008 06:39 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
write an update query in shell prompt to update the database in sqlserver suchi_s Programming 2 09-29-2004 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:42 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