Git commands in cron job - linux

I am new to cron jobs. I want to run git commands through cron job.
I just tried a simple one with 'git status'.
I created a script
#!/bin/bash
echo git status
It is working when I run the script directly in the git view but not working in crontab.
I created the crontab as
* * * * * /path/to/script >> path/to/outputfile.
Please help me.
EDITED:
Please provide any sample program for running simple git commands in cronjob.

First off, you don't need an
echo `git status`
to output text, if you call the script with the >>. That way, you just echo the output you'd get anyway.
Concerning your problem, as was already discussed, you can give the direct path to git (such as /usr/local/bin/git). Furthermore, the script probably won't be called in the directory where your git repo lies; you have to cd there first, e.g.
#!/bin/bash
cd <wherever your repo lies>
/usr/local/bin/git status
and then add that script in your crontab as you already did. Hope that helps.

Related

shell script that matches string in git commit message and exports it

I try to write a shell script (bash).
The aim of the script is
to get the message of the last git commit
grasp any content inside []-parenthesis of the last git commit message
export that content into an environment variable called GIT_COMMIT_MESSAGE_CONTEXT
Example:
Last git commit message = "[Stage] - gitlab trial"
Working example: The environment variable exported should be
echo $GIT_COMMIT_MESSAGE_CONTEXT
Stage
I found the following, to get the message of the last git commit:
echo $(git log -1 --pretty=%B)
[Stage] - gitlab trial
I am new to bash-scripts and therefore my trial (see below) is somewhat poor so far.
Maybe somebody has more experience to help out here.
My bash script (my-bash-script.sh) looks as follows:
#!/usr/bin/bash
# get last git commit Message
last_git_commit_message="$(git log -1 --pretty=%B)"
export LAST_GIT_COMMIT_MESSAGE="$last_git_commit_message"
I run the bash-script in a terminal as follows:
bash my-bash-script.sh
After closing/re-opening Terminal, I type:
echo $LAST_GIT_COMMIT_MESSAGE
Unfortunately without any result.
Here my questions:
Why do I not get any env-variable echo after running the bash script ?
How to deduct the content of []-parenthis of the last git commit message ?
How to re-write my script ?
The script seems fine, but the approach is flawed. Bash can only export variables to subshells but not vice versa. When you call a script a new shell is started. All variables in that shell, even the exported ones, will be lost after the script exits. See also here.
Some possible ways around that problem:
Source the script.
Let the script print the value and capture its output: variable=$(myScript)
Write the script as a bash function inside your .bashrc.
Depending on what you want to do I recommend 2. or 3. To do 3., put the following in your ~/.bashrc (or ~/.bash_profile if you are using Mac OS) file, start a new shell and use the command extractFromLastCommit as if it were a script.
extractFromLastCommit() {
export LAST_GIT_COMMIT_MESSAGE=$(
git log -1 --pretty=%B |
grep -o '\[[^][]\]' |
tr -d '\[\]' |
head -n1 # only take the first "[…]" – remove this line if you want all
)
}
bash my-bash-script.sh
Starts a new bash process into which your var is exported, then it exits and takes its environment with it.
source my-bash-script.sh
Executes the script in the current shell context and will have the desired effect.

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...

Trying to run script through crontab which won't work

I'm having issues getting my crontab to run I have the following line added to my crontab -e but it won't start. The command runs fine if I run it manually.
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py
Not getting any error messages and can't see the process when I run top or grep for it.
Usually this happens because the cron environment is different from your own. Make sure your start.py script uses full paths to any referenced files or external scripts. Make sure that your start.py script does not rely on environment variables that you have in your shell but it may not. Try piping the cron output to a mail command so you can see what it is doing, like so:
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py | mail -s "cron output" myself#example.com
An easier way to troubleshoot this is to write a wrapper shell script and send the output to a log file.
Create file python_start_cron.sh with contents
#!/bin/bash
cd /home/sam/p/ && /usr/bin/python3.5 start.py
Set the execute bit on this script script and make sure the script works manually
Modify the cronjob as shown below
0 */3 * * * python_start_cron.sh >/tmp/python_start_cron.log 2>&1
After cron executes, check the contents of the log file to ascertain the cause of the problem.

any way to run "git pull" in every one minute

I am using supervisor to start node server. There is options in "supervisor --help" called
-p|--poll-interval
How often to poll watched files for changes.
Defaults to Node default.
But it is not what I want. I'd like to run "git pull" in shell in every 1 minute. Is there anyway to do it?
It could be in NPM or supervisor, or something else.
Use cron job in linux/unix, and task scheduler for windows. And they're similar.
For cron job, first write a shell script to do what you want, like git pull something. Suppose it's /scripts/git_pull_job.sh, and make it executable.
chmod 755 /scripts/git_pull_job.sh
Then add the job. In the terminal input:
crontab -e
Then in the VI style editor, input things like below, and save and close as VI. It will run every minute.
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * /scripts/git_pull_job.sh
why not just run a cron job? git is not a node functionality, it's an OS functionality. Use an OS tool.

Creating a text file via SSH shell

I am trying to create a BASH shell script that runs through SSH on my shared hosting account to automate the git website control system detailed in:
http://danielmiessler.com/study/git/#website.
So far my bash script is right out of the above article:
cd ~/mydomains; mkdir $name.git;
cd ~/mydomains/$name.git; git init --bare;
/bin/vi ~/domains/$name.git/hooks/post-update
The first 2 lines work as expected.
when I add the third line the script seems to freeze up. the path to vi is /bin/vi in my environment.
Directly from the article I want the to perform the following:
vi /path/website.git/hooks/post-update
then insert:
GIT_WORK_TREE=/path/htdocs git checkout -f
Then close the file.
Could anyone offer me some advice on what to do now?
Why are you try to use vi in script just to add line into file?
Problem is that vi is interactive, but you're looking how to automate this ;-)
You should use
echo "GIT_WORK_TREE=/path/htdocs git checkout -f" >> ~/domains/$name.git/hooks/post-update
I believe you need to make the hook script executable before git will use it.
chmod +x /path/website.git/hooks/post-update
After that, after pushing, the script ought to be executed.

Resources