This question already has an answer here:
crontab failed to run python script at reboot
(1 answer)
Closed 1 year ago.
I've started a compute engine instance in Google Cloud, I created a folder called "python" in the main dir, and "one.py" in that dir. Than I pasted this into one.py:
from datetime import datetime
import os
a=datetime.now()
file_to_open = os.path.join(os.getcwd(), "raw_data.txt")
with open(file_to_open, "a+") as file_object:
file_object.seek(0)
data = file_object.read(100)
if len(data)>0:
file_object.write("\n")
file_object.write(str(a))
file_object.close()
So far, so good. Dates are being saved to the file.
Than I added this to cronetab:
crontab -e
...
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * python3 ~python/one.py
The cron job is working, I'm getting this outputs after running
grep CRON /var/log/syslog
Mar 21 11:33:01 instance-2 CRON[605]: (myname) CMD (python3 ~/home/python/one.py)
Mar 21 11:33:01 instance-2 CRON[604]: (CRON) info (No MTA installed, discarding output)
Mar 21 11:34:01 instance-2 CRON[647]: (myname) CMD (python3 ~/home/python/one.py)
Mar 21 11:34:01 instance-2 CRON[646]: (CRON) info (No MTA installed, discarding output)
I thought it might be sth with the root directory being in a different place so did find . raw_data.txt
there's only one, under ~python/
Can someone help me fix this? Why the cron job isn't saving the dates to the file?
My assumption is that it is looking for raw_data.txt in the wrong folder and failing because it cannot find the file. To see the full about of the script, add a log file for the cron job to dump the output to. You could say something like: * * * * * /use/bin/python3 /home/m/one.py >> /home/m/out.log. This would dumb the full output of the execution to out.log and give you all the information you need to solve the issue.
Without knowing more, my assumption is that the issue is caused by os.getcwd(). This does not get the current working directory of one.py but the current working directory where cron is executed from. Since you are opening as a+, the file must exist first in order to append to the file. What you want instead is os.path.dirname(os.path.realpath(__file__)) which will give you the full directory path of one.py regardless of where the script is called from. Change that line to be this:
file_to_open = os.path.join(os.path.dirname(os.path.realpath(__file__)), "raw_data.txt")
Related
This question already has answers here:
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
So I just recently started having interest and playing CTF's on OverTheWire website and I am still in the first challenge called the bandit Lvl5 if you'll consider looking at it. So it teaches the use of linux command line etc
So here on this challenge I am tasked that there are hidden files in home directory I have to find them and in one of those files I will find a password to go to the next level taking note the are over 20 hidden files that come up.
So I then thought no man yes I can go through this manually but it will take forever so I tried making a script that I thought would work to get the hidden files and open one by one. But it does not work as I wanted.
I wanted to append those hidden files into a list and then run a for loop that will open each one of them and then I will be able to see results and spot password
Code Below
import os
a = []
for i in os.system('find .inhere/'):
a.append(i)
for j in a:
print("\n\n cat j ")
So it my first time messing code of such manner trying to interact with the command line using python can you please help on how I can go about it or if my code can be fixed
os.system() only returns the exit status of the command (Not the STDOUT). You should use the subprocess module especially the subprocess.Popen. I have added several comments for my code for the better understanding.
Code:
import subprocess
import sys
def call_command(command):
"""
Call a command the STDOUT
:param command: The related command
:return: STDOUT as string
"""
result1 = subprocess.Popen(
command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
# Get the STDOUT and STDERR descriptors of the command.
std_out, std_err = result1.communicate()
return std_out
# Find files in test1 folder.
find_result = call_command("find test1/ -type f")
for one_find in find_result.split("\n"):
if one_find:
# The result of cat command will be in "cat_result" variable.
cat_result = call_command("cat {}".format(one_find))
print(cat_result)
# You can write the result directly to STDOUT with the following line.
# sys.stdout.write("\n".join(call_command("cat {}".format(one_find))))
Content of test1 folder:
>>> ll test1/
total 8
drwxrwxr-x 2 user grp 4096 Jul 8 14:18 ./
drwxrwxr-x 18 user grp 4096 Jul 8 14:33 ../
-rw-rw-r-- 1 user grp 29 Jul 8 14:18 test_file.txt
Content of test_file.txt:
>>> cat test1/test_file.txt
Contents of test_file.txt file
Output of the code:
>>> python3 test.py
Contents of test_file.txt file.
Today, I was testing my old python script, it was about fetching some details from an API and write then in a file. Until my last test it was working perfectly fine but today when I executed the script it worked, I mean no error at all but it neither write nor created any file. The API is returning complete data - I tested on it terminal, then I created another test.py file to check if file write statements are working, so the result was - they were not working.
I don't know what is causing the issue, it also ain't giving any error.
This is my sample TEST.PY file
filename = "domain.log"
with open(filename, 'a') as domain_file:
domain_file.write("HELLO\n")
domain_file.write("ANOTHER HELLO\n")
Thank you
Using 'a' on the open call to open the file in append mode (as shown in your code) should work just fine.
I don't think your issue is on the Python side. The next thing to check are your directory permissions:
$ ls -al domain.log
-rw-r--r-- 1 taylor staff 60 Apr 16 07:57 domain.log
Here's my output after running your code a few times:
$ cat domain.log
HELLO
ANOTHER HELLO
HELLO
ANOTHER HELLO
HELLO
ANOTHER HELLO
It may be related to file permission or its directory. Use ls -la to see file and folder permissions.
I am absolutely new to ubuntu and cron.
I want to run some scripts:
I edited and saved the crontab file:
37 13 30 6 * /media/xxx/xxx/bin/python /home/xxx/PycharmProjects/testcron.py
testcron.py code:
print('Hellow World')
input('Test Success')
I assumed that this would show me if the cronjob ran.
But no window popped up on the time I set.
Can someone point me to how to check if it ran? Did I configure this wrong?
If you haven't done anything on your system, try looking in
/var/log/syslog
Use grep to filter/search:
grep CRON /var/log/syslog
You can also pipe the output of your cron job to a specific location as well
37 13 30 6 * /media/xxx/xxx/bin/python /home/xxx/PycharmProjects/testcron.p >> /var/log/job.log 2>&1
I always include date >> ~/cronjobname.txt at the end of my cron job scripts. It will keep appending the date and time to that text file every time it runs. Another advantage to this is, If someone runs it manually for any reason (testing, debugging), you will have that timestamp too.
In Python, you can create a testcron.py file that looks like this:
f = open("cool_test", "w")
f.write("")
f.close()
If you run python3 testcron.py, you'll see a cool_test file generated in the same directory as testcron.py.
Now, you can just call testcron.py from a cronjob, and you'll be able to verify the process worked by making sure that cool_file was generated.
I have a script which I run every day at particular time so and I am using cron for this task and from within cron I am also trying to redirect the output which is ".txt" file to a folder.
0 14 * * * python /home/lab/Desktop/meet.py > /home/lab/Meet1 2>&1
without using "/home/lab/Meet1 2>&1 " path I am able to run script at a given time but I am not able to redirect my output to the folder provided with the path.
Is there any way to direct my txt file output to the given folder(Meet1) or my given command is wrong.
so I need help with crontab command to redirect my txt file output to given(Meet1 in this example) folder.
Thanks for your help
You should not redirect a stream into a folder but into a file. Given that /home/lab/Meet1 really is an existing folder, try redirecting to e.g. /home/lab/Meet1/output.txt or so.
Correct answer is already provided to redirect it to a file not directory, just to add:
# Below command will overwrite contents of temp file.
0 14 * * * python /home/lab/Desktop/meet.py> temp 2>&1
# Below command will append contents to temp file.
0 14 * * * python /home/lab/Desktop/meet.py >> temp 2>&1
someone help me please:
I have made a cron job in ubuntu server 12.04, the frist job recolete data from web, and set in data base, it's working well, but in secon job the python scrpit in reading database and should make a file from database data , but it not working.
rub#myserver:/etc$ crontab -e
i've edit this:
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * /usr/bin/python /home/rub/app/writeindb.py
*/1 * * * * /usr/bin/python /home/rub/app/writedatafromdb.py
the last part of my code in python writedatafromdb.py
cursor.close() #close curso in data base
conn.close() #close connecion in data base
json.dump(geojson, open('datafromdb.json', 'w'))
You are using a relative path in your code, so the file datafromdb.json gets written to the current working directory. But a cronjob doesn't run your Python script in the same working directory as you would in the shell. See What is the 'working directory' when cron executes a job over on the Unix & Linux Stack Exchange website.
Use an absolute filepath instead; one that start with a / and spells out the full path to the file.