I want to ask you if there is something like UNIX $PATH for PHP CLI.
Eg., I want to use
php a2addvhost.php example.com
instead of
php /usr/share/php/a2addvhost.php example.com
I tried to change include_path and $PATH but either work.
If you're doing
php a2addvhost.php example.com
You're still in Unix. So the a2addvhost.php file must be in the current directory for it to work.
The first argument must be the exact pathname. However, you can make a start script (as root):
$ echo -e '#!/bin/sh\nexec php /usr/share/php/a2addvhost.php "$#"\n' \
> /usr/bin/a2addvhost
$ # And then start with ...
$ a2addvhost example.com
Alternatively, make a2addvhost.php executable by prepending it as follows:
#!/usr/bin/env php
<?php
/* php code goes here */
and making it executable:
$ chmod a+x /usr/share/php/a2addvhost.php
Now, if PATH contains /usr/share/php/, you can start your script with
$ /usr/share/php/a2addvhost.php example.com
Add /usr/share/php/ to your PATH, give it the executable flag, then simply run
a2addvhost.php example.com
you may need to add the shebang at the beginning of the file.
Related
I start almost all my scripts lately with
#!/usr/bin/env nix-shell
#! nix-shell -i bash
set -e
But this requires that the default.nix/shell.nix be in the same directory as the script which is not always the case. Is there a way to tell nix-shell to look at all parent directories for a *.nix file?
The items after #! nix-shell are basically regular nix-shell arguments, so you can add the file name like this.
#!/usr/bin/env nix-shell
#! nix-shell -i bash ../shell.nix
set -e
Path resolution happens relative to the script file.
When you use a directory path, it will look for default.nix, not shell.nix inside the directory.
Im trying to run my .sh scipt status.sh via a telegram message:
Ubuntu 20.04.1 LTS server
Telegram-cli with a lua script to action status.sh script
when i send the message "status" to my server via telegram it actions the status.sh script, in this script i have a bunch of stuff that gathers info for me and sends it back to telegram so i can see what the status of my server is, however (i recently did a fresh install of the server) for some reason if the script has a line of code starting with sudo i get:
line 38: /usr/bin/sudo: Permission denied
if i run the script from the command line ./status.sh it runs without any problem!? so im thinking its because it is being called from telegram or lua!?
example of code that generates the error: sudo ifconfig enp0s25 >> file
on the other hand this line works without a problem:
sudo echo Time: $(date +"%H:%M:%S") > file
/usr/bin has 0755 permission set
sudo has 4755 permission set
The following command
sudo ifconfig enp0s25 >> file
would not work if file requires root privilege to be modified.
sudo affects ifconfig but not the redirection.
To fix it:
sudo sh -c 'ifconfig enp0s25 >> file'
As mentioned in Egor Skriptunoff's answer, sudo only affects the command being run with sudo, and not the redirect.
Perhaps nothing is being written to file in your case because ifconfig is writing the output you are interested in to stderr instead of to stdout.
If you want to append both stdout and stderr to file as root, use this command:
sudo sh -c 'ifconfig enp0s25 >> file 2>&1'
Here, sh is invoked via sudo so that the redirect to file will be done as root.
Without the 2>&1, only ifconfig's stdout will be appended to file. The 2>&1 tells the shell to redirect stderr to stdout.
If file can be written to without root, this may simplify to
sudo ifconfig enp0s25 >> file 2>&1
On CentOS, I'm trying to pass an environment variable to a PHP script.
I've created this file, test.php:
<?php print_r($_ENV);
When I run this command:
DB=mysql php test.php
I get the following output:
Array
(
)
What did I miss?
Check your variables_order php.ini variable. It has to contain E for $_ENV to be populated. You can also do:
$ DB=whatever php -d variables_order=E -r 'echo $_ENV["DB"];'
whatever
Alternatively, you can use getenv() which will work regardless of the value of variables_order.
Use getenv function:
$ cat test.php
<?php
print_r(getenv('DB'));
?>
$ DB=msql php test.php
mysql
There has been a lot of talk already done on Stack Overflow about adding a folder to the sudo path. But, none of the other tutorials I've seen have really answered the following question:
How can I add a folder to the sudo PATH without using -i.
Here is my setup. The folder I want to add is "/var/folder". There is the bash script "/var/folder/script.sh". I added the following lines of code to the /root/.bashrc file:
if [ -d /var/folder ]; then
PATH=/var/folder:$PATH
fi
Now, when I type in the command "sudo echo $PATH" I get the following output:
/var/folder:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The problem is, when I run the command "sudo script.sh", the script can't seem to be found. The output is as follows:
sudo: script.sh: command not found
This is in spite of the fact that tab-auto-complete works on "sudo script.sh".
All though the $PATH is defined when you do an echo of it, for running the script it is not actually defined. So to see what is happening, you can run the following:
sudo -s
echo $PATH
You will notice that it will be:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Where is it getting the $PATH from?
It is defined in your sudoers file:
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Solution
You can update the Defaults secure_path in the sudoers file to have the correct value.
Defaults secure_path="/var/folder:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Going back to how you were seeing the correct value when you ran:
sudo echo $PATH
Since you had $PATH defined with /var/folder before you ran that command, it just replaced the $PATH with the value, but your actual path for sudo was
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
so you were effectively running
sudo echo /var/folder:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
I'm trying to create a autostart script to start a Rails Instance Server when my SUSE Linux Server is restarting.
I've created a shell script in /etc/init.d/rails_s_appname with the following content:
#!/bin/bash
/home/appname/public_html/rails s -p 3333 -d
I gave the script 755 permissions and started it.
The result is the following:
/etc/init.d/rails_s_appname
/etc/init.d/rails_s_appname: line 2: /home/appname/public_html/rails: No such file or directory
Any one has an idea how to start a Rails Instance Server from an absolute path?
It's better do not use the rails script to launch your app like that.
Try Thin or unicorn. Both have the cwd configuration to say where is you APP_HOME
But if you really want do that. Use the script/rails commandline inside your APP_HOME to do what you want
/home/appname/script/rails s -p 3333 -d