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?
Related
I've installed frequent-cron, according to the installation instructions on https://github.com/homer6/frequent-cron.
I've changed /etc/init.d/frequent_service/init_script.tpl for my own needs:
# This is arbitrary. Change it to whatever you'd like.
SERVICE_NAME="frequent_service"
# Frequent cron must exist at this path. Optionally, you can change it to a path where it does exist.
EXEC=/usr/bin/frequent-cron
# Change this to the path of your choice.
#PIDFILE=/var/run/${SERVICE_NAME}.pid
PIDFILE=/var/run/${Odoo_OPC}.pid
# Point this to the shell (or program) that you'd like to run. /tmp/myshell.sh is just an example.
#COMMAND=/tmp/myshell.sh
COMMAND=python /usr/bin/OPCConnectie/OdooNaarOPC.py
# Frequency is in milliseconds. The command will be invoked every frequency interval.
FREQUENCY=1000
But when I want to start frequent-cron with:
sudo /etc/init.d/frequent_service start
It gives these errors:
hew#hew-VirtualBox:~$ sudo /etc/init.d/frequent_service start
/usr/bin/OPCConnectie/OdooNaarOPC.py: 1: /usr/bin/OPCConnectie/OdooNaarOPC.py: import: not found
/usr/bin/OPCConnectie/OdooNaarOPC.py: 2: /usr/bin/OPCConnectie/OdooNaarOPC.py: Syntax error: "(" unexpected
Starting frequent_service frequent cron...
/etc/init.d/frequent_service: 32: /etc/init.d/frequent_service: /usr/bin/frequent-cron: Permission denied
frequent_service started.
Even after:
sudo chmod -R 777 /usr/bin/OPCConnectie/
sudo chmod -R 777 /etc/init.d/frequent_service
It gives the same errors.
I think I can split this problem in 2 problems.
1) Error about the python script:
I don't know what's wrong because it runs fine in a normal cronjob.
Is there anything wrong with how I want to start the script in init_script.tpl?
2) Error Permission denied:
I always thought chmod -R 777 gives the folder + all subfolders and files ALL permissions? So why permission is still denied?
Edit: Checked the permissions as exussum recommended:
hew#hew-VirtualBox:~$ namei -m /etc/init.d/frequent_service
f: /etc/init.d/frequent_service
drwxr-xr-x /
drwxr-xr-x etc
drwxr-xr-x init.d
-rwxrwxrwx frequent_service
hew#hew-VirtualBox:~$ namei -m /usr/bin/OPCConnectie/
f: /usr/bin/OPCConnectie/
drwxr-xr-x /
drwxr-xr-x usr
drwxr-xr-x bin
drwxrwxrwx OPCConnectie
Are these correct? Or, should these permissions be enough?
Edit 2: Answer to problem 1 (python)
Had to put the following line on top of the python script:
#!/usr/bin/env python
Found here: Crontab wont run python script
Still need permissions for /usr/bin/frequent-cron though.
It's likely you don't have permission (x) on one of the paths leading to it.
namei -m /your/path/here
will show all permissions of the path to that point.
Edit
On second look looks like your missing a shebang line
#!/usr/bin/python
at the top of the file.
I need to run a bunch of scripts (with sudo) that use a single file.sh as a configuration file for all. Initially I've put the file.sh in /etc/profile.d and when I ran the scripts as root everything was ok (because when I connected to the machine it first sourced the file.sh and all vars in that file were available) but now, for security reasons, I need to run them with another user with sudo rights.
When running with sudo the "configuration file" in /etc/profile.d does not get sourced even if I'm root and do sudo - it's the same.
Using "sudo -E" is not an option, also this kind of solution "Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"" does not work for me as the vars in the file change a lot and it's easier to throw a file, with all the vars, in a location - like /etc/profile.d/ - instead to adding options to /etc/sudoers.
Later Edit (working):
Moved original sudo command to sudo.orig. Created a new sudo bash script
[root#NS1 bin]# cat sudo
#!/bin/bash
source /etc/profile.d/set_env_vmdeploy.sh
sh /usr/bin/sudo.orig "$#"
and gave it permissions
[root#NS1 bin]# chmod 4111 sudo
[root#NS1 bin]# ll sudo*
---s--x--x 1 root root 78 May 7 13:42 sudo
---s--x--x 1 root root 123832 Jul 31 2014 sudo.orig
If you want sudo to execute all the profile scripts in the child shell, you can tell it to invoke the shell as a login shell: sudo -i /usr/local/bin/my_script.sh. (Note that the child shell will start with the working directory set to /root, and also that this may have other unintended side effects.)
Alternatively, invoke bash explicitly with a command parameter: sudo /bin/bash -c "source ./config.sh; ./real_script.sh".
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.
I have a script which calls some other bash, python, and ruby scripts. The script is similar to the following:
#!/bin/bash
set -x
./aux1.py
./aux2.sh
When I run the script from the command line, it runs but it gives me the following output:
./script.sh: line 5: ./aux1.py: Permission denied
The aux.py script has #!/usr/bin/env python at the top; when I call it from the command line with ./aux.py it is fine, however when called from this script in the exact same way I get this error. The strange part is that this only happens when I run the script with ./script.sh; when I run the script with bash script.sh the auxilliary scripts work perfectly.
I'm using Linux, why does this happen?
Update, 3 March 2014
There's nothing conflicting in the PATH, and all of the scripts are chmod +rx. uname -srvmo gives
Linux 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Dec 13 06:58:20 EST 2013 x86_64 GNU/Linux
and there's no more information available in uname -a. SELinux is enabled, but I'm not an administrator so I can't access that information.
Before launching the main script you must be sure that all the subscripts have got the right permissions. In particular you are interested in the permission for you (your user and owner of the files) to execute the file. This is a short tutorial on permissions.
http://blog.pluralsight.com/linux-file-permissions
http://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions
If this is the problem (and it look so), the simpler solution is to run the following commands
chmod u+x script.sh aux1.py aux2.sh
To check whether you have permission to execute simply run
ls -l
You should see something like
-rwxr--r-- 1 youruser youruser 26 Feb 28 21:44 script.sh
-rwxr--r-- 1 youruser youruser 32 Feb 28 21:44 aux1.py
where you should see a "x" in the 4th column corresponding to the file.
Maybe type sudo su before running your script so that you are the root user. When the script completes simply type exit to get out of root. Type exit again to close the shell.
:D
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);