Apr-09-2020, 06:22 PM
(Apr-09-2020, 06:18 PM)ibreeden Wrote:Any user can run the script, I think I found the issue had two problems and have updated my code to the following. Im not sure if this is the most sensable way to do so but it appears to be working. If you have any change suggestions im open to them(Apr-09-2020, 05:49 PM)tpolim008 Wrote: The folder in which Im putting the output text into has a permissions of 7777 so anyone can read and write to itI think there is a 7 too much in your permissions. Now you also have setUID and setGID and sticky bit. You don't want that. Use chmod 777 or 0777.
Who is executing the script? Also root?
the file folder is /opt/scripts/output where I have forgot the last / it should have been /opt/scripts/output/
next was I gave two arguments where I guess this write function can only have one,
ERROR write() takes exactly one argument (2 given)
here is the new code.
#! /usr/bin/python3.6 import pymysql import datetime from pncnetworkdata import * from datetime import date try: markets = ["Beltway", "Keystone", "Freedom", "New England"] count = 0 cursorRowCount = 0 recordPrint = "" for HOST in HOSTS: db_con = pymysql.connect(host=HOST, port=3306, user=USERNAME, passwd=PASSWORD, db=DATABASE) sql_select_Query = "SELECT * FROM pncnetwork where id=(SELECT MAX(id) FROM pncnetwork)" cursor = db_con.cursor() cursor.execute(sql_select_Query) records = cursor.fetchall() for row in records: # recordPrint = "" recordPrint += markets[count] + ', ' + str(row[0]) + ', ' + str(row[5]) + "\n" cursorRowCount += cursor.rowcount count += 1 db_con.close() # print ("Total number of rows in pncnetwork is: ", cursorRowCount) print ("\nPrinting each pncnetwork record\n", recordPrint) # This section will print the script to file using current date. def get_filename_datetime(): # Use current date to get a text file name. return "file-" + str(date.today()) + ".txt" #Get full path for writing name = get_filename_datetime() print("NAME", name) path = "/opt/scripts/output/" + name print("PATH", path); with open(path, "w") as f: # Write Data to file f.write (recordPrint) except Exception as e: print("ERROR ", str(e))