Crontab 不更新 .txt 文件中的变量
Crontab doesn't updates variable in .txt file
我的 python 脚本有问题。
#!/usr/bin/python
import sys
import MySQLdb
import os
import time
import datetime
import glob
jaki = 0
plik = open('aktualny.txt')
otwarcie = open('Pomiary.txt')
try:
pomiar = plik.read()
czest = otwarcie.read()
except:
print ("nie ma takiego pliku")
finally:
czestotliwosc = int(czest)
ile = int(pomiar)
if ile == czestotliwosc-1:
ile = 0
plik = open('aktualny.txt','w')
pomiar = str(ile)
plik.write(pomiar + '\n')
plik.close()
otwarcie.close()
jaki = ile
else:
ile = ile + 1
plik = open('aktualny.txt','w')
pomiar = str(ile)
plik.write(pomiar + '\n')
plik.close()
otwarcie.close()
jaki = ile
plik = open('aktualny.txt','w')
plik.write(pomiar + '\n')
plik.close
如果我从终端 运行 这个脚本,一切正常(这意味着 "aktualny.txt" 是当前值),但是如果我使用 crontab 每 1 分钟执行一次这个脚本,"aktualny.txt" 为空,没有变量。我给了所有文件和脚本的所有权限,但它并没有解决问题。
在 "Pomiary.txt" 中我有常量“2”。
有什么问题吗?
已关注 Martijn Pieters 的 post:"You are using relative paths for the files you open. The current directory when running a crontab will differ from when you run it directly. Use absolute paths for the files and you'll see the script is working fine."
我的 python 脚本有问题。
#!/usr/bin/python
import sys
import MySQLdb
import os
import time
import datetime
import glob
jaki = 0
plik = open('aktualny.txt')
otwarcie = open('Pomiary.txt')
try:
pomiar = plik.read()
czest = otwarcie.read()
except:
print ("nie ma takiego pliku")
finally:
czestotliwosc = int(czest)
ile = int(pomiar)
if ile == czestotliwosc-1:
ile = 0
plik = open('aktualny.txt','w')
pomiar = str(ile)
plik.write(pomiar + '\n')
plik.close()
otwarcie.close()
jaki = ile
else:
ile = ile + 1
plik = open('aktualny.txt','w')
pomiar = str(ile)
plik.write(pomiar + '\n')
plik.close()
otwarcie.close()
jaki = ile
plik = open('aktualny.txt','w')
plik.write(pomiar + '\n')
plik.close
如果我从终端 运行 这个脚本,一切正常(这意味着 "aktualny.txt" 是当前值),但是如果我使用 crontab 每 1 分钟执行一次这个脚本,"aktualny.txt" 为空,没有变量。我给了所有文件和脚本的所有权限,但它并没有解决问题。
在 "Pomiary.txt" 中我有常量“2”。
有什么问题吗?
已关注 Martijn Pieters 的 post:"You are using relative paths for the files you open. The current directory when running a crontab will differ from when you run it directly. Use absolute paths for the files and you'll see the script is working fine."