LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2017, 12:32 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
Trouble running PHP script in Desktop Launcher


Hi,

I'm trying to run this php script (
Code:
php -f Desktop/transferPhotos.php
) in a Desktop Launcher.

It works fine in the terminal.

--------------------------------------

As the command in a Desktop Launcher:

I have tried
Code:
./Desktop/transferPhotos.php
RESULT: Terminal "blinks".

I have tried
Code:
bash Desktop/transferPhotos.php
with & without the ./ in front of "Desktop"

RESULT: Terminal "blinks".

I have tried
Code:
bash-c Desktop/transferPhotos.php
with & without the ./ in front of "Desktop"

RESULT: Terminal "blinks".

Other commands I have tried:
RESULT: "There was an error launching the application"

Permissions are set to 777.

Thanks in advance for any help in solving this.

R

Last edited by pizzipie; 05-02-2017 at 12:36 PM.
 
Old 05-02-2017, 01:19 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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.
 
1 members found this post helpful.
Old 05-02-2017, 04:11 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thank you astrogeek for your quick response,

Quote:
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.
 
Old 05-02-2017, 06:31 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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...

Code:
php -f /home/rick/Desktop/transferPhotos.php; bash -i
That will hold the presses until you close the terminal window or type exit at the prompt.
 
Old 05-02-2017, 07:46 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Here is what I'm doing:

Code:
<?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!!

R
 
Old 05-02-2017, 08:25 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Try this first...

In the php file, change the very first line to look like this...

Code:
#!/usr/bin/php
<?php
error_reporting (E_ALL ^ E_NOTICE);
...
(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...

Code:
xterm -e /home/rick/Desktop/transferPhotos.php; bash -i
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.

Last edited by astrogeek; 05-02-2017 at 08:26 PM.
 
Old 05-03-2017, 12:08 AM   #7
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Maybe there's a problem that certain environment variables are not set properly when you run it that way vs. running from a login shell.
 
Old 05-05-2017, 01:39 PM   #8
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks astrogeek and Laserbeak

None of the above worked.

The xterm actually ran but closed before you could see the results on the screen

Running the launcher just produced the message "There was an error launching the application."

I don't have a clue as what environment variables would be involved here.

Thanks for your time.

R
 
Old 05-05-2017, 02:29 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Another try:
Code:
sh -c "xterm -e \"/home/rick/Desktop/transferPhotos.php; bash -i\" "
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Having trouble running tsch script in bash protolocke Linux - General 3 11-02-2010 12:22 PM
Running Bash script in PHP script SteveMack2015 Linux - Software 2 05-14-2010 03:34 PM
Desktop Launcher of shell-script in gnome-terminal skipidar Linux - Newbie 1 06-24-2009 09:27 AM
Running a command from a launcher (on gnome desktop) Virtuality Linux - Newbie 1 12-17-2007 08:32 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration