I am trying to run a bash script as user apache. and it throws up the following
[apache#denison public]$ ll
total 32
drwxr-xr-x 2 apache apache 4096 Jul 17 08:14 css
-rw-r--r-- 1 apache apache 4820 Jul 17 10:04 h3111142_58_2012-07-17_16-03-58.php
-rwxrwxrwx 1 apache apache 95 Jul 17 10:04 h31111.bash
drwxr-xr-x 2 apache apache 4096 Jul 17 08:14 images
-rw-r--r-- 1 apache apache 754 Jul 17 08:13 index.php
drwxr-xr-x 2 apache apache 4096 Jul 17 08:14 javascript
drwxr-xr-x 5 apache apache 4096 Jul 17 08:14 jquery-ui-1.8.21.custom
[apache#denison public]$ bash h31111.bash
: command not found :
contents of the file are:
#!/bin/bash
/usr/bin/php /opt/eposdatatransfer/public/h3111142_58_2012-07-17_16-03-58.php
php script runs fine below are the results
[apache#denison public]$ /bin/bash h31111.bash
: command not found:
[apache#denison public]$ chmod +x h31111.bash
[apache#denison public]$ ./h31111.bash
./h31111.bash: Command not found.
[apache#denison public]$ php h3111142_58_2012-07-17_16-03-58.php
creation of file:
$batchFile = $this->session->username . "_" . $index . "_" . $date . ".sh";
$handle = fopen($batchFile, 'w');
$data = "#!/bin/bash
/usr/bin/php /opt/eposdatatransfer/public/$file
";
/*
rm -rf /opt/eposdatatransfer/public/$file
rm -rf /opt/eposdatatransfer/public/$batchFile*";*/
fwrite($handle, $data);
fclose($handle);
batchfile is the bash script and file is the php file. These get craeted automatically based on user input in webapp. my webapp runs on linux.
I'm guessing you uploaded the script from a windows machine, and didn't strip the carriage returns from the end of the lines. This causes the #! mechanism (the first line of most scripts) to fail, because it searches for #!/some/interpreter^M, which rarely exists.
You can probably strip the carriage returns, if you have them, using fromdos or:
tr -d '\015' < /path/to/script > /tmp/script; chmod 755 /tmp/script; mv /tmp/script /path/to/script
What happens if you try to run your script with
$ /bin/bash h31111.bash
Try this (assuming your script file is named "h31111.bash"):
$ chmod +x h31111.bash
then to run it
$ ./h31111.bash
Also are you sure you have the right path for your php command? What does which php report?
--
As #jordanm correctly suggests based on the output of the file command I suggested you run, you need to run the dos2unix command on your file. If you don't have that installed this tr -d '\r' will also work. I.e.,
$ tr -d '\r' h31111.bash > tmp.bash
$ mv tmp.bash h31111.bash
and you should be all set.
Under some versions of Ubuntu these utilities (e.g., dos2unix) don't come installed, for information on this see this page.
It looks to me the problem is your $PATH. Some users on the system will have . (the current directory) in their $PATH and others will not. If typing ./h31111.bash works, then that's your problem. You can either specify the file with a relative or absolute path, or you can add . to the $PATH of that user but never do that for root.
Since you're not sure where it's failing, let's try to find out.
First, can you execute the program?
./h31111.bash
That should be equivalent to invoking it with:
/bin/bash h31111.bash
If the above gives you the same error message than it's likely a problem with the script contents. To figure out where something's gone awry in a bash script, you can use set -x and set -v.
set -x will show you expansions
set -v will show you the lines before their read
So, you'd change your script contents to something like the following:
#!/bin/bash
set -x
set -v
/usr/bin/php /opt/eposdatatransfer/public/h3111142_58_2012-07-17_16-03-58.php
Another possibility, which you probably only learn by experience, is that the file is in MSDOS mode (i.e., has CR LF's instead of just LFs). This often happens if you're still using FTP (as opposed to SFTP) and in ASCII mode. You may be able to set your transfer mode to binary and have everything work successfully. If not, you have a few options. Any of the following lines should work:
dos2unix /path/to/your/file
sed -i 's/\r//g' /path/to/your/file
tr -d '\r' < /path/to/your/file > /path/to/temp/file && mv /path/to/temp/file /path/to/your/file
In Vim, you could do :set ff=unix and save the file to change the format as well.
Let's take a look at what you have in PHP:
$handle = fopen($batchFile, 'w');
$data = "#!/bin/bash
/usr/bin/php /opt/eposdatatransfer/public/$file
";
Since you have a multi-line string, the CR LF characters that are embedded will depend on whether your PHP file is in Windows or Unix format. You could switch that file to Windows format and you'd be fine. But that seems easy to break in the future, so I'd go with something a little less volatile:
$handle = fopen($batchFile, 'w');
fwrite($handle, "#!/bin/bash\n");
fwrite($handle, "/usr/bin/php /opt/eposdatatransfer/public/$file\n");
fclose($handle);
Related
Today, my working bash scripts stopped working on my Debian 11 server.
$ ./CCrec.sh
-bash: ./CCrec.sh: Permission denied
Fails even if using sudo.
Yes, the permissions are set correctly. (They've been working for years.)
$ ls -l CCrec.sh
-rwxr--r-- 1 user1 user1 858 Jan 23 20:30 CCrec.sh
Another clue: autotab it doesn't recognize the script is present with beginning with "./". This makes me think the's a change in my bashrc, but I'm not noticing a change.
script will run when specifying bash:
$ bash CCrec.sh
This is working
Any solutions?
I am trying to run commands over ssh from Linux machine to Windows machine.
Windows machine have OpenSSHx64 installed
Following command with double quotes fails:
ssh user#win8-pc "ls -l \\\\172.21.15.120\\vol0slash"
ls: cannot access 172.21.15.120vol0slash: No such file or directory
The same command with single quotes still fails, but at least display single slash:
ssh user#win8-pc 'ls -l \\\\172.21.15.120\\vol0slash'
ls: cannot access \172.21.15.120vol0slash: No such file or directory
Surrounding path with single quotes almost works, but still one root slash is missing:
ssh user#win8-pc "ls -l '\\\\172.21.15.120\\vol0slash'"
ls: cannot access \172.21.15.120\vol0slash: No such file or directory
Now finally adding fifth slash to UNC path root, did the trick:
ssh user#win8-pc "ls -l '\\\\\172.21.15.120\\vol0slash'"
total 536
drwxr-xr-x 1 Admin Domain Users 0 Jan 23 08:33 GeneralSystemDiagnostic
drwxr-xr-x 1 Admin Domain Users 0 Jan 22 08:10 cifs
-rw-r--r-- 1 Admin Domain Users 336 Jan 23 12:00 linux.txt
drwxr-xr-x 1 Admin Domain Users 0 Jan 19 14:11 nfs
Can anyone explain the logic behind this behavior?
Backslash is special symbol in bash and quite much in all Linux shells, therefore if you need to use it, it has to be escaped using another \ (backslash). The command is passed through the remote bash such as
bash -c "ls -l '\\\\\172.21.15.120\\vol0slash'"
which transfers evaluates the special characters and makes it look like
ls -l '\\\172.21.15.120\vol0slash'
when it is supposed to run.
The problem with using odd number of backslashes will end up evaluating as a special character, so you should go with even number, if you want to see a backslash in the end.
The other thing is how the arguments are interpreted by ls on Windows (which I have no idea about). See the tests with simple echo:
$ ssh f25 "echo '\1'"
\1
$ ssh f25 "echo '\\1'"
\1
$ ssh f25 "echo '\\\1'"
\\1
$ ssh f25 "echo '\\\\1'"
\\1
Similarly you can explain the original command without ':
ssh user#win8-pc "ls -l \\\\172.21.15.120\\vol0slash"
gets already in local shell (because it is not in ')
ssh user#win8-pc "ls -l \\172.21.15.120\vol0slash"
and remote shell gets already
bash -c "ls -l \\172.21.15.120\vol0slash"
which evaluates to
bash -c "ls -l \172.21.15.120vol0slash"
and to
ls -l 172.21.15.120vol0slash
which is obviously not existing.
I have a simple script file to copy all files to a remote server and then delete them all. I could run this script by "user" manually, when i add into crontab (user), the first part, scp, works fine, but the rm part is always with failure.
i wonder what i am missing or set up incorrectly, could somebody help me out with this ?
thanks in advance
/home/user/bin/test.sh
#!/bin/bash
scp -v -r /var/spool/asterisk/monitor test#xx.xx.xx.xx:/home/test/audio&&sudo rm -f /var/spool/asterisk/monitor/*
access permission of /var/spool/asterisk/monitor
drwxr-xr-x. 1 root root 532 Sep 06 11:14 monitor
crontab - user]
* */1 * * * bash /home/user/bin/test.sh
try this, it will work if sudo does not require password ( and it is possible ) )
scp -v -r /var/spool/asterisk/monitor test#xx.xx.xx.xx:/home/test/audio && ssh test#xx.xx.xx.xx "sudo rm -f /var/spool/asterisk/monitor/*"
Make sure requiretty is off in /etc/sudoers. It is normally on by default on Red Hat.
I am very puzzled by this problem I am having. I am trying to execute a file in Ubuntu 12.04 LTS via command line. I have a script that calls a program to run and write the results in a hard drive. I changed the permissions and ownership of everything to be wxr. Here is the ls -l of my script (called TEST-star):
-rwxrwxrwx 1 root root 950 Nov 15 13:16 TEST-star
Here is the ls -l of the package my script calls:
-rwxrwxrwx 1 root root 1931414 Nov 10 12:37 STAR
Finally the ls -l of the hard drive mounted in /media/CLC"
drwxrwxrwx 1 root root 8192 Nov 15 13:04 CLC
I have been trying to run it since yesterday and always get a message that I don't have permission to write the results:
EXITING because of FATAL ERROR: could not create output file ./_STARtmp//Unmapped.out.mate1.thread14
Solution: check that you have permission to write this file
I thought if I change the permissions to rwx and run my script as root it would not have a problem (using sudo). Right now I run out of options. Any suggestion would be appreciated. Please let me know what other information you would need solve this issue.
Thank you.
Here is the first line of script I am trying to run:
#!/bin/sh
cd /media/CLC/ANOPHELES-STAR-v2.4f1/; mkdir GambFemAnt1 && cd GambFemAnt1; echo $PWD && echo Starting mapping of GambFemAnt1; /home/aedes/Documents/STAR_2.4.0f1/STAR --genomeDir /media/Galaxy/Galaxy_data/Anopheles/STAR/Genome --readFilesIn /media/Galaxy/Galaxy_data/Anopheles/QC/GambFemAnt1/GambFemAnt1.fastq --runThreadN 23 --outFilterMismatchNmax 4 --outFilterMatchNminOverLread 0.75 --seedSearchLmax 30 --seedSearchStartLmax 30 --seedPerReadNmax 100000 --seedPerWindowNmax 100 --alignTranscriptsPerReadNmax 100000 --alignTranscriptsPerWindowNmax 10000 --outSAMstrandField intronMotif --outFilterIntronMotifs RemoveNoncanonical --outSAMtype BAM SortedByCoordinate --outReadsUnmapped Fastx; mv Aligned.sortedByCoord.out.bam GambFemAnt1.bam; mv Unmapped.out.mate1 GambFemAnt1-unmapped.fastq; cp *.fastq /media/CLC/ANOPHELES-STAR-v2.4f1/UNMAPED-reads/; cd /media/CLC/ANOPHELES-STAR-v2.4f1 && echo $PWD && echo GambFemAnt1 mapping finished;
I also posted a question for the authors of the package.
Turns out all the permissions were set correctly. The problem resigns within the package. I found out that it works using --runThreadN 12 instead of --runThreadN 23.
To start learning BASH scripting, I've created a trivial script that curls down a stock price from YAHOO and prints it to STDOUT. I've set the permissions to rwx for everyone, and moved the file into my path:
Script:
root#raspberrypi:~/code/scripts$ cat quote
#!/bin/bash
while getopts "s:" opt; do
case $opt in
s)STOCK="$OPTARG";;
\?) echo "ERROR"; exit 1;;
esac
done
PRICE=$(curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=${STOCK}&f=l1")
echo "${STOCK}: ${PRICE}"; exit 0
exit 0
I then set the permissions for all users:
root#raspberrypi:~/code/scripts$ chmod 777 quote
Here is my $PATH:
root#raspberrypi:~/code/scripts$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
I now move it into my path in what I read was the appropriate location for custom user scripts:
root#raspberrypi:~/code/scripts$ ls -la /usr/local/bin
total 12K
drwxrwsr-x 2 root staff 4.0K Apr 30 01:28 ./
drwxrwsr-x 10 root staff 4.0K Jan 1 1970 ../
-rwxrwxrwx 1 root root 433 Apr 30 01:22 quote*
The which command will find it (expected):
root#raspberrypi:~/code/scripts$ which quote
/usr/local/bin/quote
PROBLEM: when I run the script, it returns the first option on the next line followed by my prompt:
root#raspberrypi:~/code/scripts$ quote -s aapl
'-s'root#raspberrypi:~/code/scripts$
But, when I run the script with a full path, it works just fine:
root#raspberrypi:~/code/scripts$ /usr/local/bin/quote -s aapl
-s: 592.33
Apologies if this less a programming question and more a Unix question, but I want to make sure I rule out a problem with my code before I do anything else.
I'm sure this is something very easy, so thanks in advance for the extra set of eyes.
You just chose an unfortunate name for your script. quote is also the name of a function from the bash_completion package, and functions override external scripts.
type quote will show that it's a function (whereis doesn't know about these things).
Either rename it or disable bash_completion. Or run command quote every time.