LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help writing logon script to access and update a database (https://www.linuxquestions.org/questions/linux-newbie-8/help-writing-logon-script-to-access-and-update-a-database-674319/)

K.out 10-05-2008 03:48 AM

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?

Mr. C. 10-05-2008 04:51 AM

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.

K.out 10-06-2008 03:05 AM

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">'

Mr. C. 10-06-2008 03:52 AM

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

K.out 10-06-2008 09:12 AM

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.

Mr. C. 10-06-2008 01:26 PM

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

K.out 10-09-2008 11:19 AM

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

Mr. C. 10-09-2008 12:27 PM

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.

K.out 10-14-2008 10:29 PM

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

Mr. C. 10-14-2008 11:41 PM

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?

K.out 10-15-2008 10:39 AM

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.

Mr. C. 10-15-2008 11:21 AM

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

evaluatinglinux 10-15-2008 02:51 PM

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

Mr. C. 10-15-2008 04:32 PM

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.

evaluatinglinux 10-17-2008 01:45 PM

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.
:)


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