running git from crontab - permission denied - linux

I've looked this up and found lots of answers but I am a unix/linux dummy. Not able to follow peoples instructions. Not sure which files to edit etc..
need simple step by step instructions here
I have a shell file set up like so:
#!/bin/bash
exec &>> /var/www/nginx/yokohama/laravel/storage/cron.log
today='date +%Y-%m-%d.%H:%M:%S';
cp /var/www/nginx/yok/yok_data.xml /var/www/nginx/yok/yok_XML_Files/backup-$(date +"%Y_%m_%d").xml
git add /var/www/nginx/yok/yok_XML_Files/backup-$(date +"%Y_%m_%d").xml
git commit -m "cool test"
git push origin staging
if I run it manually as root it works and we're all good. but doesnt work through my cron..
here is my crontab
* * * * * /usr/bin/sh /var/www/nginx/yok/laravel/commit_data.sh
In my cron.log I'm getting
Permission denied (publickey).^M
How can I fix this? Simple instructions.

Permission denied (publickey).^M
The ^M at the end that your script might have \r\n eol (end of lines) characters instead of \n: try dos2unix.
Also make sure your script is executable:
chmod 755 /var/www/nginx/yok/laravel/commit_data.sh
The OP hamobi mentions another reason in the comments:
problem even more basic. had to move users rsa key into bitbucket.

Related

Ubuntu move-cli gives me /usr/bin/env: ‘node\r’: No such file or directory [duplicate]

I have Rails project. When I try to run any rake task or rails server it give me this error
env: ruby\r: No such file or directory
Could someone help me?
If you are working on a Unix / Mac, then this error is because you have incorrect line endings.
Here is a solution using dos2unix; you may need to install this program on your system. If apt is available, you can use sudo apt install dos2unix.
Set your line endings correctly, and have git manage how it handles them:
git config --global core.autocrlf input
In your directory you are going to convert all of the files by running:
find ./ -type f -exec dos2unix {} \;
This will cycle through all of your files, converting them. and solving the problem. Add your changes. Commit them, and you should be good to go.
You probably have edited ./bin/rake file and added \r at the end of first line:
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require_relative '../config/boot'
require 'rake'
Rake.application.run
Make sure that after "ruby" you have only new line char.
Thanks to the comments above, I solved my server issue that was caused from cloning my group's github rails app and causing localhost:3000 to fail. I was just working on the backend from my fullstack app: ruby(-v 2.7.1)/rails(-v 6.0.3.4). And these 2 people's comments solved my error:
"For those of you who got "find: ‘dos2unix’: No such file or directory" error: sudo apt install dos2unix" – RealMan Jul 26 '17 at 14:59
"Note that that find command may be excessive... this point is arguable; it may well be fine, but it may be overkill in some situations. Another possible route (for step 2 in this answer) is git rm -r --cached . followed by git reset --hard HEAD... which is likely faster (if nothing else, it won't run dos2unix on files in the .git housekeeping directory!)... This has potential gotchas as well (probably quite fine if you're running from a "clean" checkout, though), but thought I'd at least mention it." – lindes Jul 13 '19 at 0:42
I kept getting this error and finally figured out how to fix it.
I made sure all the permissions on the files in my bin folder were
executable.
Run ls -lha in your current repository. You want each file to have an x at the end like this
-rwxr-xr-x.
To achieve this, you will want to run chmod +x <file_name_here> for each file in your bin folder, such as chmod +x rails, chmod +x bundle, etc.
Now when you run ls -lha you should see that they all have an x at the end.
Next, either in SublimeText, Atom or what ever text editor you have, you will want to check that you are not using Windows line endings. The \r character is something Windows uses. Unix just uses \n for a new line.
I use Atom so I went to the plugins section (Cmd + , on Mac) and then searched for line-ending-selector in the Packages section, and then went to the line-ending-selectors settings. Change your default to 'LF'.
You will find that at the bottom of files, Atom will tell you the type of line ending the file is using with a CRLF for Windows and LF for Unix/Mac. You want all your files to use 'LF'.
So in your terminal, open each file in your bin folder in Atom, by running atom ./bin/filename (such as atom ./bin/rake).
At the bottom you will see 'CRLF' or 'LF'. If you see 'CRLF', click on it and, at the top of Atom, you can choose 'LF'.
Cmd + s to save.
Do this for each. You are basically telling your file to strip all Windows line endings and use Unix line endings instead.
Once all files are edited, you should be able to run your rake or rails command.
Note: Sublime Text and Text Mate should have equivalents to Atom's line-ending-selector.
For macOS users
Step 1: HOMEBREW_NO_AUTO_UPDATE=1 brew install dos2unix
Step 2: git config --global core.autocrlf input
Step 3: find ./ -type f -exec dos2unix {} \; (in the repo you were trying to run your task on)
git add and git commit
You are good to go!
If none of the works, try this:
git config --global core.autocrlf true
rails app:update:bin
I had the same problem on Windows Terminal, using WSL 2! I followed a post that recommended to install the dos2unix dependencie: sudo apt install dos2unix (Using apt package manager) and run other two commands:
git config --global core.autocrlf input (Set your line endings correctly, and have git manage how it handles them)
find ./ -type f -exec dos2unix {} \; (In your directory you are going to convert all of the files)
The git will identify a couple of changes, but you don't need to commit it. I just made a git restore . , remove node dependencies rm -rf node_modules and download it again yarn install.

Alias Multiple Commands in Linux

I am managing a website using git. One of the requirements for the git repository is that bare = true. It uses a post-receive hook to manage pushes from my local computer. The problem is that sometimes I would like to make changes to a WordPress directory on my website using the wp-admin view online. So then I would just ssh into the directory and run git --work-tree="BLAH" add . and git --work-tree="BLAH" commit -m "BLAH". Is there a way to set up an alias, like alias git="git --work-tree=\"BLAH\"" and have that work for all git commands?
There are times when alias are a great tool. Then there are times when things start getting too complicated where a shell script is better.
To create a single command that executes other commands just create a file (maybe call it git-add-all) then type the following:
#! /bin/bash
git --work-tree="BLAH" add .
git --work-tree="BLAH" commit -m "BLAH"
Then you can run the script by simply doing:
bash git-add-all
Even better, make the script executable:
chmod +x git-add-all
Then you can use it like any command:
./git-add-all
Advanced tips:
To be able to run the script from any git directory you can copy/move the file to one of the directories in your $PATH. For example /usr/loca/bin. Then you can simply run git-add-all instead of ./git-add-all.
Even better is to create your own personal scripts directory and include it in $PATH. I personally use ~/bin. To add the directory to $PATH you just need to add the following to .bashrc or .profile:
export PATH=/home/username/bin:$PATH
or if you're doing this for the root user:
export PATH=/root/bin:$PATH
In case anyone is curious how I solved it (thanks to shellter's comment), I wrote a bash script then prompted the user for input like so:
#!/bin/bash
function fix {
git --work-tree="PATH_TO_WORKING_TREE" $1
}
echo -n "git "
read -e INPUT
until [ "$INPUT" = "quit" ]; do
fix $INPUT
echo -n "git "
read -e INPUT
done
Running it:
user#server [repo.git] $ git-fix
git status
# On branch master
nothing to commit (working directory clean)
git quit
There is a .bashrc file in Linux. You can edit it for creating alias for your favorite and frequently used commands.
To create an alias permanently add the alias to your .bashrc file
gedit ~/.bashrc
The alias should look like:
alias al='cmd'
You can read more about it over here.

Unix Bash script causing git clone to not work

I have a bash script (script1.sh) where I perform a git clone.
Then, from that repository, I run a another script (script2.sh) which runs fine.
I run script2.sh just fine, but the git repo is non existant. Any folder just isn't there. If I run the git clone on the command line, it clones it just fine.
Why is my script no git cloning my repo correctly?
How I run the first script.
sudo bash script1.sh
script1.sh
cd /home/ubuntu
git clone http://mygit-thing.com/myrepository.git localfolder
#run script from the repo
sudo bash localfolder/script2.sh
script2.sh
~ Some unrelated unix commands
Notes: I tried looking at the /home/ubuntu folder and could not find it. It's not a "hidden" file as well.
This could solve your problem replace your script1.sh:
home=/home/ubuntu
folder=$home/localfolder
git clone http://mygit-thing.com/myrepository.git $folder
#run script from the repo
bash $folder/script2.sh
if it does not work, it is maybe possible that you are not allowed to write on $home because of your current user permissions or because your fs is read-only You can check that by executing mount without option, it will list all mounted fs.
Another point, sudoing from inside a script is not recommended. Currently you are basically sudoing on a sudo. If you want to be sure the right user is executing your script, you better check the current id than doing nested sudo.
As #jibe suggested, you're calling bash from sudo, which will call bash from different location. Provide full path to the local git repository.
I have same issue during to write blue-green deployment script , I resolved calling with bash.
#!/bin/bash
bash /home/ubuntu/deployment-guru/green-deployment.sh
green-deployment.sh
#!/bin/sh
cd /home/ubuntu/my_api && git pull origin blue-green-jenkins-integration
before calling without bash i was facing following issue
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

Git: Working with executables

I have a project in which there is a .sh file and it has to be in the executable mode once pulled by the others. Now, I already changed its permissions on my local machine. However I want it to be pushed/pulled as executable as well, so that the other users do not have to run chmod command all the time.
I have been informed about two possibile solutions: .gitattributes and git update-index --chmod=+x script.sh, however I am not sure what exactly should I follow, given my condition.
I've seen this post here and this answer there, and I am thinking which one would suit my case more. I want this process to be done automatically, not by the user everytime, added.
Any thoughts?
Since you've tagged this question with linux, you can just check the file in. Now that you've changed it on your computer:
% chmod +x script.sh
Git will notice that the file has changed:
% git status
On branch old2
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: script.sh
And you can diff to see that the file is, in fact, now executable:
% git diff foo.sh
diff --git a/foo.sh b/foo.sh
old mode 100644
new mode 100755
Mode 100644 reflects a non-executable file, and mode 100755 reflects an executable file, similar to the Unix file permission bits. (Had you changed the file in addition to changing the permissions, you would also see the changes.)
When you check this in and your collaborators pull your changes, the file will be made executable for them, too.
Note that this does not work automatically on systems that do not have the notion of an execute bit (ie, Windows), and in that case, you will need to do something with update-index.
Note also that this relies on your system to have configured core.filemode being set correctly. This value should be set to true on all non-Windows systems.
% git config core.filemode
true
If, for some reason, that command returns false and you are on a non-Windows computer, run:
% git config core.filemode true
to re-enable it.
You have to decide which is fit for you. Two methods those you given above they can acceptable and they almost the same.
You should know how to give working permissions to executable files. In Linux, true way to do it is "chmod". Also you can sue git hooks as well. For doing in normal method I preferred this:
git add file.sh #this could be py file or something
git update-index --chmod=+x file.sh #make it executable
git commit -m "here the commit" #commit it
git push #push it
So if you want to do it another way you should try this:
#!/usr/bin/bash
chmod +x file.sh
And you can run it. But before the run it, you should give working permissions to your script that you made for giving working permissions to other scripts :)
Or you can give the permissions dynamically:
su -c 'chmod +x file.sh'
In this way, you should give the working permission for one time and it runs.
Did you consider Git hooks?
Maybe this could be solution for you
.git/hooks/post-checkout:
#!/bin/sh
chmod +x script.sh
Here you can find more about Git hooks.

Creat Cron Job to Reapply Permissions on QNAP NAS

I have a QNAP NAS running Google Drive sync so that my QNAP, Computers and Google Drive are all in Sync.
When I create a file on my work computer and get home to the QNAP I get an access denied error on the file I created at work.
If I view the permissions I can see they are set incorrectly. From the QNAP web manager I simply right click the folder containing my files and set permissions to "Reapply and apply to subfolders/files".
How would one go about doing the above via a cron job that runs say every 5 minutes?
I had a similar problem myself and also made a cron job for it.
start of with making a script in a easy to find place.
I used "/share/MD0_DATA/" because all the shares live here.
Create a file like perms.sh and add the following:
#!/bin/bash
cd /share/MD0_DATA/(folder you want to apply this)
chmod -R 775 *
chown -R nobody:nogroup *
I used the nobody:nogroup just for example you can use any user and group you want.
Now you need to add this script to crontab.
To see whats in your crontab use:
crontab -l
to edit the crontab use:
crontab -e
This editor works like vi if you don't like vi and want to access the file directly edit:
/etc/config/crontab
Add this line to your crontab:
*/5 * * * * /share/MD0_DATA/perms.sh
The 5 represents a 5 minute interval.
Then you need to let crontab know about the new commands:
crontab /etc/config/crontab
I hope this helped you.

Resources