I am having major issues with nfs, in that server-1 has to mount a file from server-2 before an application owned by another user can operate. The applications will appear to load without the mounted share but they will be just going through the motions and the application will not actually be working. The troubleshooting is difficult because the end users always send trouble tickets about the wrong thing.

I wanted to develop a script that would check to see if the mount was present and if it wasn't present, run the mount script. Since the application started by the other user would need to be restarted, I want to automatically restart those pieces, as their proper user, and then keep the checking script checking every 5 minutes for status. If the mount is still present, the script will just do nothing.

This must be somewhere where the root and the spacial user can gain access, and so for testing sake it is in my own user $HOME directory

This is where I am at the moment:

import os
import time

def main():
	x = 5
	y = 0
	while y < x:
		waa = os.system('mount | grep "sda18"')  # Checks for existing mount
		# maa = os.system('mount') # This line produces a print line of output of mount
		print waa, "<- This is the content of the waa variable"
		if waa == 256:
			#print('blagh')
			f = open('tettt.txt','a')
			eee = os.system('echo `date` >> tettt.txt')
			f.write('\n')
			#f.write('eee')
			f.close()
			#print file('tettt.txt').read()  # Scaffolding
			s = 's'
			s = sysuser(s)
			print '%s is the user\n' % (s)
		time.sleep(3)
		x = x-1
def sysuser(s):
	s = str(os.system('whoami'))
	L = 'should be a user name'
	print s, L
	if  str(os.system('whoami')) == 'root':
		os.system('su - halton')
		print os.system('whoami'), L
		os.system('touch file.1.txt')
		os.system('ls -l')
		os.system('exit')
	elif os.system('whoami') == 'halton':
		print 'I am the Halton UsEr'
		print os.system('whoami'), L
		os.system('touch file.1.txt')
		os.system('ls -l')
		os.system('exit')
	else:
		print 'cannot become special user'
	return s
main()
#Successful output (mount is present)
halton@halton-1:~/Desktop/innovator/bin/LyrasisCode$ ./mounty.py 
/dev/sda5 on / type ext4 (rw,errors=remount-ro)  # This should not be printing at all
0 <- This is the content of the waa variable




Output as root: (mount is missing)
256 <- This is the content of the waa variable
root
0 should be a user name
root
root
cannot become special user
0 is the user


==============
output as halton user  (should be going to the elif)
256 <- This is the content of the waa variable
halton
0 should be a user name
halton
halton
cannot become special user
0 is the user  #output of line 21

=============

What I expected the system outputs for whoami to be:
halton@halton-1:~/Desktop/innovator/bin/LyrasisCode$ whoami
halton
halton@halton-1:~/Desktop/innovator/bin/LyrasisCode$ sudo whoami
root

If the mount is present, waa=0, if it is not present, waa=256, so it is apparently returning 0 for Boolean True and 256 for False. When I set the test to a "sda18," which is not present in the output of mount, the script is writing the proper environmental date variable content to the file noted.

Why isn't the waa variable value a string?
Why isn't the s variable a username string?

You are not setting variables from command output but return value of system call function, which should be set from commands error code. I would not use system codes but check for example existence of mount point sda18 directory in /media directory in my Ubuntu distributions case.

Computers use numbers not strings. To check for a certain user I would use os.getuid(); root=0 and all other users=some other number. You can print the value for the halton user to find that user's number. I think the hack to get the name is something like
pwd.getpwuid(os.getuid())[0]
Also, you can modify the /etc/fstab file to allow a group to mount the drive, which halton would be in, and eliminate the test for halton or root.

Here are a few hints: first try to use python's library functions instead of calls to os.system. The following works for me

import os, pwd, time

def whoami():
    return pwd.getpwuid(os.getuid())[0]

def become_user(name): # this seems to work when I'm root and want to become somebody else
    uid = pwd.getpwnam(name).pw_uid
    os.setuid(uid)

def append_date(filename):
    with open(filename, "a") as fh:
        fh.write(time.strftime("%c"), time.localtime())
        fh.write("\n")

If you must call system commands, use subprocess.call(), or this small class http://www.daniweb.com/software-development/python/code/257449

You are not setting variables from command output but return value of system call function, which should be set from commands error code. I would not use system codes but check for example existence of mount point sda18 directory in /media directory in my Ubuntu distributions case.

I see. I will look at your approach. I have gotten a working bash script to do this script's job, but I really want to do more in python.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.