LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getting error on bashrc (https://www.linuxquestions.org/questions/linux-newbie-8/getting-error-on-bashrc-4175445940/)

ESWBitto 01-16-2013 06:13 PM

Getting error on bashrc
 
Ok so here is what I did..

Distro 6 Centos

Goal: To be able to start kippo when user logs in.

What I have done is in the .bashrc file I added this

. /home/username/kippo-0.5/start.sh

and in the .bash_profile file I added this

if [ -f /kippo-0.5/start.sh ]; then
. /kippo-0.5/start.sh
fi



when I log into the user I get this...



Starting kippo in background...Traceback (most recent call last):
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/application/app.py", line 631, in run
runApp(config)
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/scripts/twistd.py", line 23, in runApp
_SomeApplicationRunner(config).run()
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/application/app.py", line 374, in run
self.application = self.createOrGetApplication()
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/application/app.py", line 439, in createOrGetApplication
application = getApplication(self.config, passphrase)
--- <exception caught here> ---
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/application/app.py", line 450, in getApplication
application = service.loadApplication(filename, style, passphrase)
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/application/service.py", line 400, in loadApplication
application = sob.loadValueFromFile(filename, 'application', passphrase)
File "/usr/lib64/python2.6/site-packages/Twisted-10.2.0-py2.6-linux-x86_64.egg/twisted/persisted/sob.py", line 203, in loadValueFromFile
fileObj = open(filename, mode)
exceptions.IOError: [Errno 2] No such file or directory: 'kippo.tac'

Failed to load application: [Errno 2] No such file or directory: 'kippo.tac'


So yeah...I don't know what I did wrong...I"m wondering if each file its showing is a dependency or if there is something that is more simple.

PTrenholme 01-16-2013 06:34 PM

Well, that error message clearly states that kippo.tac could not be found. That's where the Python program sob.py was told to look for that data values it needed.

I'm not clear why you're sourcing the kippo script instead of just running it. (The period before the script is the source command. Omit it if you want the script executed as a command, or replace it by the exec command if you want control transferred to the script.)

<edit>
When the bashrc script is executed, it is normally run with the working directory set to the user's home directory. If the data file is not there, you could symlink to it from your home directory, or do a cd to the correct directory before you run the start script.

I.e., something like this:
Code:

cd kippo-0.5
sh start
cd ~

might be what you need.
</edit>

ESWBitto 01-17-2013 12:23 PM

Thank you for your help I got it running by doing this in bashrc

cd /home/username/kippo-0.5
/home/username/kippo-0.5/start.sh
cd ~


Now how would I go about only having it ran once....scenario...If server was rebooted and had to log user back in....do the script...then if for some reason had to be logged out...then logged back in I don't want it to execute again.

ESWBitto 01-17-2013 01:15 PM

I think I answered my own question this is what I put...


if [ -f /home/username/kippo-0.5/kippo.pid ]; then
echo "Kippo is already running"


else


cd /home/username/kippo-0.5
/home/username/kippo-0.5/start.sh
cd ~

fi

Do you see any issues with that compared to my scenario?

I'm new to creating scripts in bash... :/

suicidaleggroll 01-17-2013 02:55 PM

Quote:

Originally Posted by ESWBitto (Post 4872386)
I think I answered my own question this is what I put...


if [ -f /home/username/kippo-0.5/kippo.pid ]; then
echo "Kippo is already running"


else


cd /home/username/kippo-0.5
/home/username/kippo-0.5/start.sh
cd ~

fi

Do you see any issues with that compared to my scenario?

I'm new to creating scripts in bash... :/

Yes, all you're checking for is the existence of a file to see if the process is running, that's not enough. Who's creating the file? What's in it? Who's making sure that if the process is no longer running the file is removed? The way you have it, if whatever creates that file abandons it by mistake, hard shutdown, etc., your script will never start up the program again.

.bashrc really isn't the place to be putting startup calls for programs that should only be run once on boot, neither is .bash_profile. /etc/rc.local is a good place (if it can be run as root), or you can use the @reboot directive in cron if your version supports it, or various other methods:
http://www.cyberciti.biz/faq/linux-e...system-reboot/

ESWBitto 01-17-2013 02:58 PM

I think it would work in this instance...Kippo can't be run as root.

the pid file it creates puts it in the kippo-0.5 directory and when the process isn't running the pid file isn't there.


I have some other programs that I would have to automate and I think your advice can help me there.

suicidaleggroll 01-17-2013 03:08 PM

Quote:

Originally Posted by ESWBitto (Post 4872433)
the pid file it creates puts it in the kippo-0.5 directory and when the process isn't running the pid file isn't there.

Sure, the program probably removes its own pid file when it closes on its own volition, but what happens if it crashes, or if the machine experiences a lock up or hard shut down and kippo doesn't get the chance to remove its pid file?

Just checking for the existence of a pid file isn't enough. You should look inside the file, chances are it contains the actual PID of the process. In which case your startup script should pull the PID out of the file and actually check if that PID is running, and if the process running with that PID has the correct name. Only if the PID contained in that file is currently running with the correct name should you skip the startup, otherwise it could just be an abandoned PID file that's preventing your script from ever launching the code.

ESWBitto 01-17-2013 03:11 PM

I see what your saying...ok let me ask you this. I have looked at the pid itself and the pid number changes...so what would I do from there?

suicidaleggroll 01-17-2013 03:16 PM

How often does it change? Have you checked ps or top to see what the process with that PID is?

Every time the process starts it's going to be given a different PID, is that what you're referring to or does the PID in that file actually change while the same instance of kippo is still running?

ESWBitto 01-17-2013 03:21 PM

Yes that's what I'm referring to....It doesn't change when that instance of kippo is running.

suicidaleggroll 01-17-2013 03:29 PM

Ah alright, that's normal then. That's the reason why you should check that both the PID in that file is running AND that it has the right name. The kernel just arbitrarily assigns out PIDs in numerical order as processes are started, then when it hits the limit (32768 IIRC) it resets back to 1 and starts over.

It's entirely possible for kippo to be started with a PID of say 4000, then at some point it crashes and abandons the file. Some time later (hours, days), some other process that's started could be arbitrarily given a PID of 4000. So you want to make sure that when you check if the PID in that file is running, that you verify it's actually kippo using that PID and not some other process that came along hours/days after kippo abandoned the file and was arbitrarily assigned the same identifier.

It's rare for that to happen, yes, but if you want to be thorough that's the best way.

chrism01 01-17-2013 04:51 PM

I'd like to second suicidaleggroll.
When I started writing permanent daemon programs I cam across exactly this issue when writing cron watchdog progs to check/restart the main daemon.
These days I never bother with pid, even if stored in a file; I always check the exact process name.

ESWBitto 01-17-2013 04:54 PM

ok so I have another one for you...

say I have a program that I want to run on root....when I edit the rc.local file and put in the same statement it works....but what I am wanting to do is take your advice and check for the pid itself rather than a file... and then echo a response to let me know either way...like this


if [ -f /var/run/program.pid ]; then
echo "Program Already Activated"


else


cd application
executable
echo "program Has been Launched :)"
cd ~


fi

PTrenholme 01-18-2013 09:37 PM

Code:

$ id -u
1000
$ sudo id -u
0

And here's a program I sometimes source when I need root access. (Things like testing SysV init scripts, etc.)
Code:

$ cat Scripts/bash/CheckForRootAccess
#!/bin/bash
function CheckForRootAccess()
{
  local whoami
  #
  # Check that sudo works for the user. Bypass the check if we're running as "root."
  #
  # Are we running as "root?"
  whoami=$(id -un)
  [ "${whoami}" = "root" ] && return 0
  #
  # Not "root." Confirm that the user has sudo permission.
  #
  echo You need root access to run this script. \(I.e.: You must be in the sudoers file.\)
  echo If prompted, enter your password for "sudo" to use.
  sudo echo OK - now we have root access.
  [ 0 != $? ] && echo Aborting. && return 1
  echo
  echo Note:      Many commands used in this scrip are prceeded by a "sudo" directive.
  echo            On most Linux system a "sudo" is only "good" for a few minutes, so it is
  echo            possible that you may receive additional "Password:" prompts as this
  echo            script is run.
  return 0
}



All times are GMT -5. The time now is 11:57 PM.