Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want my web server to run a .sh script that runs a series of .jar files. I want this to happen as soon as the server is contacted without any input from the user. A PHP or JS script would be preferable. Is it even possible to do this?
Thanks in advance
You can also look at gearman.org. It is safer and more flexible than shell_exec().
It is not doable in JavaScript, but you can use PHP for it. If I understand your question well, you have some utilities written in Java and you don't care about their output. The shell script would look like as follows:
#!/bin/bash
java -jar your-jar1.jar
java -jar your-jar2.jar
...
and provided the name of the script is myScript.sh and it has the x flag set, you can run it from PHP by:
shell_exec('path-to-script/myScript.sh');
<?php
exec ("sh your_script.sh");
?>
place this in your webserver root as a php file and replace your_script.sh with the name of your script.
WARNING: if you don't protect the script with a password, using for example a .htaccess file for Apache (http://httpd.apache.org/docs/current/howto/htaccess.html), anyone can execute that script.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to set up my system so that php can be run without including the shebang in every file. Is there an alternative to using #!/usr/bin/php in every php file I write?
The shebang is only needed, if a PHP should be called directly by a shell/tool like any other shell script. Examples are functions system() or exec*() provided by many languages, including PHP.
If calling a command, the tools look for a shebang to decide, which interpreter is to call. This works for awk too:
#!/usr/bin/awk -f
BEGIN { print "begin" }
The general idea is, to put commands e.g. in /usr/local/bin, make chmod a+x and use them like any other command without need, to select the needed interpreter manually.
Conclusion
Shebang is not needed for web server files, that are never called as script by a shell or external tool.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on a program that utilizes Python scripts that pull information from a MySQL database. The MySQL database is managed from a PHP site interface (so HTML forms and buttons and the such). I have this all set up on a raspberry pi and it works. But I want to add some more functionality.
Specifically I want to be able to execute certain python scripts from the PHP site. I want it to be as simple as a press of a button, literally.
Is this a scenario where I should use Django? I've never used it before but have read about how it connects Python with the web. I found an answer to a similar question but I'm wondering If I need to set up anything special on my apache server: https://stackoverflow.com/a/31811462/5609876
I even made a little picture for a visual representation of my program incase my explanation wasn't good enough:
No, you do not need django at all.
If all you want to do is execute a Python script from PHP - assuming you have already written the script and stored it somewhere:
First, assign execute permissions on the Python script to the user that is running the PHP code. Normally, this is the same user that is running Apache. Usually this is called www-data or apache or something similar. The user will be listed in the Apache configuration file.
Then, on your PHP side, all you really need is exec:
<?php
exec('/path/to/python /path/to/your/script.py')
?>
If the shell_exec function is allowed on your server, you can use that to run your Python script through Bash. shell_exec returns the output of the bash call. The only thing you have to make sure of is that shell_exec isn't disabled in your server's php.ini file (look for the line disable_functions shell_exec).
If your python script is called mypythonscript.py and is in the same directory as the PHP file, you can run it like this:
<? shell_exec('python mypythonscript.py'); ?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've recently been working on a web crawler with PHP and as a newish PHP coder, I'm not the most advanced. I also know a decent bit of Python and there are a few things I am able to do in Python yet not in PHP.
Is there any way for me to run a Python function with parameters inside a PHP script? Please be specific in your answers as I'm not amazing at PHP.
Run the Python script using php's shell_exec function
Think of shell_exec as basically running commands in your terminal. So if you normally run your python script from the shell like this:
/python-scripts/myscript.py "here is an argument"
You would pass the same command into shell_exec, like this:
$output = shell_exec("python-scripts/myscript.py 'here is an argument'");
Note: shell_exec will return the output of the command you run, so save it to a variable if you want to use the output from the python program in your PHP script.
Edits: fixed syntax issues, provided a more specific example
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've got a simple script that sends email if you refresh the page. I need it to run every week. I setup a command in CPanel to run that script
php /home/site/public_html/test/sendmail.php
But it does not work. Hosting support says I setup cron in CP correctly.
I wander if I need any intermediate script to run sendmail.php. Thank you for your help in advance.
Most likely you'll need to expand your cron call to /usr/bin/php /home/site/public_html/test/sendmail.php, because the system can't find the path to the php executable.
You can also add a PHP shebang to the script and run it like a normal shell script without php -f:
#!/usr/bin/php
<?php
//your code
?>
To get the PHP path, use this code: <?php echo PHP_BINDIR, PHP_EOL; ?>
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
UPDATE: I do not know Perl or Python as I have said. Don't give me examples because I don't understand it!
I have looked at other posts here, but do not see any real answer. I use Go Daddy and would like to backup my MySQL database every day, and if possible only allow 7 backups in the folder at once.
Folder: _db_backups
DB Name: ccadatabase
Go Daddy says:
Your Shared Hosting account supports the following languages and associated interpreter lines:
Perl: #!/usr/bin/perl
Python 2.2: #!/usr/bin/python2.2
Python 2.3: #!/usr/bin/python2.3
Python 2.4: #!/usr/bin/python2.4
Ruby: #!/usr/local/bin/ruby
Bash: #!/bin/bash
The installed versions of PHP 4 and PHP 5 do not support the interpreter specification in a PHP file. In order to run a PHP script via Cron, you must specify the interpreter manually. For example:
PHP 4: /web/cgi-bin/php "$HOME/html/test.php"
PHP 5: /web/cgi-bin/php5 "$HOME/html/test.php5"
Is there anyone who can help me write a quick script to make this process automated? That would be great! I don't know any Perl/Python, so if you could explain your code I would really appreciate it. Thank you!
here's what I use
#!/bin/bash
DATE_NOW=$(date +%Y%m%d)
BACKUP_DIR=/Users/nico/${DATE_NOW}
mkdir -p ${BACKUP_DIR}
echo "Dumping data ..."
mysqldump -ume -hlocalhost -ppass db > ${BACKUP_DIR}/${DATE_NOW}-mybackup.sql
Note: you'll have to change the mysqldump variables with user password db etc