ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You should tell us what desktop and which launcher to be complete.
But there are a few obvious problems with your example: Path, language interpreter and permissions.
First the path. In the terminal you have an implicit working directory from which relative paths such as Desktop/transferPhotos.php and ./Desktop/transferPhotos.php have meaning. Your desktop does not have that same path context, so you should use the full absolute path (/path/to/script...) or the path from your home directory with leading '~/'. If you are going to use GNU/Linux you will need to learn the meaning and use of path expressions at some point. The above link is an excellent place to start.
Next, the language interpreter...
In the terminal, you are already in a bash shell, and the command php -f Desktop/transferPhotos.php tells it invoke the php interpreter to run the script. You have not included the php in the launcher examples, so it presumably opens a non-interactive shell in which to run the script, but it doesn't know what language interpreter to use so it exits. Being non-interactive there is no way for it to tell you what went wrong so you just see the blink. Either specify the language interpreter in the launcher, or better yet, add it to the first line of the script itself, like this...
Code:
#!/usr/bin/php
Then when the launcher opens an execution shell it will know what to do with the file contents.
See man bash, COMMAND EXECUTION section for the explanation.
Finally, you have been a member since 2005 so you have surely seen advice to never set a file permission to 777. That is ALWAYS a bad idea and it is NEVER helpful in troubleshooting problems and USUALLY causes you to assume things about the execution environment which have no basis in reality. Please don't do that as it confuses you, can lead to serious problems on your system, and causes us to needlessly repeat this warning.
If you add the language interpreter to the top of the script, set permissions on the script to 755 at most.
If you specify the language interpreter in the command then the script needs only read permissions, 644 will do.
You should tell us what desktop and which launcher to be complete.
I am using Ubuntu 14.04 with Gnome Desktop and Launcher.
Quote:
But there are a few obvious problems with your example: Path, language interpreter and permissions.
Path: I have changed the path to absolute path /home/rick/Desktop/transferPhotos.php
Language Interpreter: I am not sure where to put '#!/usr/bin/php' before <?php or after it. In either case it doesn't seem to do anything with or without it.
Permissions: I normally use the strictest permissions I can. I changed them, in this case, to 755.
Well it seems that the program does run after all. After reading about Launcher Commands I got the idea of using sleep() to see if that would slow things down.
I am using "php -f /home/rick/Desktop/transferPhotos.php" as the launcher command now". By putting "sleep(5)" in strategic areas of the program I was able to trace this "blink" to the last line of the program. When it gets there it just closes the terminal instead of the usual stopping with all the output showing on the terminal.
So, the problem now becomes how can I get the terminal to remain open so I can see the output when I use the Desktop launcher.
As you are using Gnome there "could be" other considerations due to Gnome-terminal... if so someone else will need to chime in about that...
But, what happens is that a shell is opened as the execution environment, the php script runs, and on completion the execution shell is done... so it exits.
The simple way to hold the terminal open is to give it something else to do. You could just add an input handler, either to the php or in the bash script with a message like "Press any key to exit...", then exit the script normally. But I suspect that you might want to type other commands, or do something else, so I would just add an interactive shell to the end of the command list, like this based on your launcher command...
<?php
error_reporting (E_ALL ^ E_NOTICE);
/* ===========================================================================
transferPhotos.php v1.0 30Apr2017 - Transfer Photos for Processing by "digikam"
Search for a directory in /$user/media/ that contains the camera directories
$user is used to determine whose computer is being used.
===========================================================================*/
$file="";
$user=get_current_user();
$desktop="/home/".$user."/Desktop"; // incoming dir
$mediaDir="/media/".$user; // change to /media dir
// ================ Find the camera =================
chdir($mediaDir);
$items=scandir($mediaDir);
foreach($items as $item) {
if($item != "." && $item != "..") {
$camera=$item; // get camera name from SD memory card
}
}
$cameraDir=$mediaDir."/".$camera;
if( ! isset($camera) || $camera != "CANON_DC" && $camera != "PANASONIC") {
echo "\nERROR: No Camera Found - Check SD Card.\n";
echo "\nYou can't have more than one SD Card mounted either.\n\n";
echo "I'll wait Until you Get Back ... BYE\n\n";
exit();
}
chdir($cameraDir);
// =============== Get Photo List ===============
$dcimDir=getcwd()."/DCIM"; // change dir to where the photos are
$items=scandir($dcimDir);
echo "\nList of Possible Source Files\n\n";
foreach($items as $item) {
if($item != "." && $item != "..") {
echo $item."\n";
}
}
echo "\nEnter a File Name to Process From List \n\n";
$photoFile = trim(fgets(STDIN)); // reads one line from STDIN. We want correct file as source
$imageDir=getcwd()."/DCIM/". $photoFile;
chdir($imageDir); // go to where the actual photos are
// ===============Store Photos To Desktop, VB-share and Pictures =================================
$processDate=date("YMd-G:i_");
$transDir=$desktop."/".$processDate."TransferFile_".$photoFile; // Temporary Storage
// because I'm adding $processDate to the image filename this check isn't necessary
// but if I decide to not add the date it is necessary.
if(is_dir($transDir)) { // if Temporary Storage File Exists Delete it ???
if(delDir($transDir)) { // see function delDir line 93
mkdir($transDir);
}
else {
mkdir($transDir);
}
}
$src=getcwd(); // Source - /media/rick/PANASONIC/DCIM/106_PANA
// $transDir // Destination - /home/rick/Desktop/2017Apr29-12:12_106_PANA
echo
$cmd= "rsync -avz ".$src." ".$transDir;
echo shell_exec($cmd); // the goal - store images in dir on Desktop
// The following two lines store the images in 1. Permanent storage of 'raw' images
// and 2. storage in folder that digikam accesses for further processing
$cmd= "rsync -avz ". $transDir." /home/".$user."/Pictures/Pictures/00-Process";
$cmd2= "rsync -avz ". $transDir." /home/".$user."/VB-share/00-Process";
echo "\nSending Images to /home/rick/VB-share/00-Process and /home/rick/Pictures/Pictures\n\n";
echo shell_exec($cmd);
echo "\n -----------------------------------\n\n";
echo shell_exec($cmd2);
echo "\nDone ...\n\n"; // Program Done
// =============== Function delTransDir() ================
function delDir($transDir) {
echo "\n Transfer Directory Exists ...\n";
echo "\nDo You Want to Delete the Directory - Yes or No!\n";
$prompt = strtolower(trim(fgets(STDIN)));
if($prompt=="yes" || $prompt=="y") {
echo shell_exec("rm -r ".$transDir);
return true;
}
return false;
}
?>
This is weird.
If I add ;bash -i it "blinks".
If there is no SD card mounted it 'blinks'.
If there is an any kind of error, it 'blinks'. ie: Won't show syntax errors etc.
It seems if there is any thing 'wrong' it runs through the program like there was nothing else going on such as the 'read' of input from STDIN, any echo statement, the results of rsync, etc.
None of this behavior happens when I run from the Terminal.
Maybe I should just do that. I don't really need the Desktop launcher. I thought it would just be a tiny bit easier, eh!!
(Actually, on your system type which php, and use whatever path it says, may be /bin/php...)
Then in your launcher change the command to (it will no longer need the php interpreter to be specified with the above change)...
Code:
/home/rick/Desktop/transferPhotos.php; bash -i
And make sure permissions on that file are 755.
If that does not work, then it is likely something related to gnome-terminal. I saw some comments on a similar problem recently but do not know where that was. You might also try it like this to use xterm instead...
If those don't work I'll try to find the gnome-terminal problem when I return later this evening (or you could try searching for it in the mean time). Or, hopefully, a gnome-terminal user will chime in with the relevant comments.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.