fwrite, fopen, and cronjob - not saving to proper location - cron

I wrote a php script to get my latest Twitter tweet and save it to a file. Here is the part doing so :
// No errors exist. Write tweets to json/txt file.
$file = $twitteruser."-tweets.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, json_encode($tweets));
fclose($fh);
This works fine when I run my php script directly in the browser, however, when I run the file from a cron job it creates the file in my user root directory (obviously not the correct place).
If I change the above line to :
$file = "public_html/get-tweets/".$twitteruser."-tweets.txt";
the cronjob now works and saves to the correct location, but then manually running the file in my browser gives an fopen error that the file does not exist.
What the heck is the problem? I need this to work both from cronjob and manually.

Use a full path from the root of the filesystem, then both should be fine.

Related

File is not shown in the folder generated via tcl script

I am trying to write a file using tcl scripting (Via VMD). when I type command "dir" on tk/tcl console, it shows file name which I am trying to generate. But when I tried to open that file manually in that working directory folder, it is not even shown in it.
Here is the piece of code.
set fp [open "input.txt" w+]
puts $fp "test"
close $fp
Sometimes, Windows can be deeply tricky about what is going on with the actual folders.
If you do this in your script as well:
# This is quite an incantation!
exec {*}[auto_execok cmd] /c start "" [file nativename [file normalize .]]
then it should open an Explorer window on the actual current directory. That will let you check whether the file is actually there, and whether where you are is where you expect to be.
This should match up with what this says:
# Print the current working directory
puts [pwd]
# Print the names of text files in the current directory, with full names
puts [glob -directory [pwd] *.txt]
If it doesn't, that might point to your real problem…

ParaView get file path

I am opening some VTU files from Directory X and there are other output files in that directory (for example log.txt) that I want to open via a plugin. If I do a os.getcwd() I end up in ParaViews installation directory. What I want is the directory of the VTU files I loaded BEFORE applying the plugin... So basically the start Point of the Pipline.
You could do something like this to get the reader
myreader = FindSource('MyReader')
then get the file name via the FileName attribute
myreader.FileName

How to use file from folder in bash

I use this code to upload text file to the folder. It works fine, file is in folder. File is named by multimedia id -"mid.txt"(i.e: "1.txt"):
Part of upload.php:
if(isset($_POST['btn-upload']))
{
$folder="/var/www/tmp/textgrid_uploads/$mid.txt";
I want to use the uploaded file "$mid.txt" from "textgrid_uploads" directory in script for conversion. The script "conversion.sh" has defined "input", "output" and "temporary" file because it has to be defined to store temp file:
Part of script "conversion.sh":
mid=$1
infile="/var/www/tmp/textgrid_uploads/$mid"
outfile="/var/www/tmp/mlf/${mid}.mlf"
tmpfile="/var/www/tmp/mlf/${mid}.tmp"
The script works fine, becasuse I tested it for files which were created manually on server using: vim 1.txt. Problem is when I want to use script "conversion.sh" for file which was uploaded on server using "upload.php". After using script "conversion.sh" It creates blank file called "$mid.mlf" in "mlf" directory with no text. Running of "conversion.sh": i.e: ./conversion.sh 1.txt

(bash) how to add some iteraion to filename

I have the script that creates some .html and .txt files every day. But now it is only one file html and txt with changed content, I need every day a new html&txt file with date oof creation in the file name like : index_22-05-2013.html , i have these variables in shell script:
DESTINATION_HTML="./daily/html/index_$(date +"%F").html"
DESTINATION_TXT="./daily/txt/index_$(date +"%F").txt"
and a line in shell script that running one python script and creates html file
python `somescript.py` -m ${FILELIST[0]} ${FILELIST[1]} > $DESTINATION_HTML
and i`m getting this file created:
index_$(date +"%F").html
what i must to do to get this file name : index_22-05-2013.html
Sorry, I am not following you, but since
echo "index_$(date +%F).html"
outputs index_2013-08-20.html instead of index_22-05-2013.html which is what you need, you probably want to use this command instead:
echo "index_$(date +%d-%m-%Y).html"
Hope it helps! :)

PHP cron job can't create new files

I have developed a script that my company uses to back up our client's DBs on our server by creating a dump file of their DBs. Here is my code:
$result=mysql_query("SELECT * FROM backup_sites");
while($row=mysql_fetch_array($result)){
$backup_directory=$row["backup_directory"];
$thishost=$row["host"];
$thisusername=$row["username"];
$thispassword=$row["password"];
$thisdb_name=$row["db_name"];
$link=mysql_connect("$thishost", "$thisusername", "$thispassword");
if($link){
mysql_select_db("$thisdb_name");
$backupFile = $thisdb_name ."-". date("Y-m-d-H-i-s", strtotime ("+1 hour")) . '.sql';
$command = "/usr/bin/mysqldump -h $thishost -u $thisusername -p$thispassword $thisdb_name > site-backups/$backup_directory/$backupFile";
system($command);
}
}
The script gets all the client's db information from a table (backup_sites) and loops through each one to create the backup file in that client's directory. This script works great when it is manually executed. The problem that I am having is that it does not work when I set it to run as a Cron job. The email from the Cron job contains this error for each DB backup attempt:
sh: site-backups/CLIENT_DIRECTORY/DB_DUMP_FILE.sql: No such file or directory
So for whatever reason, the Cron job is unable to create/write the DB dump file. I can't figure this one out. It just seems strange that the script works perfectly when executed manually. Anyone have any ideas? Thanks in advance!
Adam
Consider using absolute pathes. Cron may have a different base directory.

Resources