Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have made a shell script on my red hat server that needs to be run every minute. My script is located in /media. I have edited the crontab like so:
* * * * * /media/statusshellscript.sh
My script is definitely in the location above and I know that 5 stars means run every minute.
oh.. and my script definintely works! because when I do a ./statusshellscript it works fine. Here is my script anyway, it basically just runs a php script I made which made life easier.
#!/bin/bash
# Script to execute the PHP Script
cd ~
cd /media/PHPServerTest
php -f index.php
Crontab is doing absolutely nothing at the moment. Not sure what to try next?
Also.. permissions shouldn't be a problem as i've done chmod 777 statusshellscript.
if its not running though cronjob but by command its working fine then there can be two reasons
1) you never made your file executable , that you can resolve my using the command
sudo chmod +x filename
2) your path is not correct , for finding absolute path you can use command
realpath(filename)
if realpath is not already installed it will mention you a command how to install it
by checking these points it should work fine.
The PATH for a crontab is not the same as in a shell.
Make sure that you define a PATH in your crontab that includes everything that is needed by the script.
Also, make sure that the script starts with a valid #! marker that points at the desired shell.
Or, use the full path for all commands in the script.
As others have said, my bet would be a misconfigured PATH. Try putting this into your path:
"* * * * * /media/statusshellscript.sh"
Go check that output file to see the PATH when the script is run. And rather than defining your PATH in the crontab, just define it in your script.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am using a custom package manager called spack, which allows me to load installed modules using the spack load command. It is similar to the familiar module load command in many ways. I am using zsh.
I have set up a shell script with a function that I would later like to insert into my .zshrc file. It is currently located in a standalone file for testing purposes, which looks as following:
#!/bin/bash
load-standard () {
echo "loading $1"
spack load $1
}
load-standard $1
When I run this script with source ./script_name package_name, I get an error message that says
`spack load` requires Spack's shell support.
To enable Spack's shell support, a file called setup-env.sh must be run which enables the user to make use of the spack command.
However, directly typing in the commands spack load package_name works with no problem.
I always assumed that running a command from a shell script is the same as typing it into the current shell. How can I make my shell interpret the spack load commands exactly as if I had directly typed them in?
EDIT: Placing the function in my .zshrc file solved this problem.
I'm not familiar with spack, but likely spack is a shell function which modifies the current shell environment. That is how module works. type spack to check.
You can't modify the shell environment from a script, you can from a shell function.
Copy and paste the function load-standard to "$ZDOTDIR/.zshrc" (for current user, /etc/zshrc for all users), source .zshrc (. "$ZDOTDIR/.zshrc") and you should be fine (no need to restart).
You can also create a list of functions in a file, and add . /path/to/functions to zshrc, to source it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't understand, I have looked at several forums to help me, etc... But it still doesn't work!
I would like to run a script thanks to Cron!
To try, I'm currently trying to execute a script that sends "test" in commands (with echo test). No problem, this script works perfectly by hand if I call the file.
But I tried to run my file with Crontab -e every minute and I've been waiting for several minutes already, but no results.
And I can't really understand why!
Already, I was told to put #! /bin/bash at the beginning of my code in my script, but when I put it in I have an error and the code doesn't execute by hand. Whereas if I don't put anything in, the code runs smoothly.
So I don't know if that's where the mistake came from, maybe.....
The final goal would be to make a script that runs every day, say to clear the cache with sync; echo 3 > /proc/sys/vm/drop_caches.
What should look like in the Crontab : 00 20 * * * PATH if I'm not mistaken.
Do you have a solution to help me?
EDIT: -bash: /root/Discord/script/cache.sh: /bin/bash^M: bad interpreter: No such file or directory it's the error I got when I runned /root/Discord/script/cache.sh to execut my script. And that command works when I don't have #! /bin/bash. But that works when I used sh cache.sh in the directory, even with #! /bin/bash !
Crontabs don't print the output on your opened terminal. You need to either create a file and then append the output there to view or test if it works. You can refer this answer https://stackoverflow.com/a/28856563/7181668
But if you want to run a shell script file through cron then you need to make sure you have given executable permission to that file and then you can use the below command in crontab -e
* * * * * /bin/sh /home/myUser/scripts/test.sh
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get into writing scripts that execute in the terminal. I wanted to know what the best way to do this was. I want to start by making a simple script that will run four or five commands that will update a certain program on my computer and have that run every day at a certain time. I have a programming background, but I am unfamiliar with this kind of scripting. I would appreciate any advice or input such as what language to use.
First of all, you need to open a Terminal (such as Terminal, Terminator, etc) and then you run this:
touch myScript.sh
chmod 755 myScript.sh
The first command creates an empty file and then you give 755 permissions to it. It means that it will be readable and executable by any user in your machine. If you need more details about those permissions, you can refer to the documentation here. But, believe me, those permissions will work for the moment.
Now you can insert instructions into the file using several methods: You can open it with a text editor such as vi, etc; Also, you can echo those commands this way:
echo "ls /tmp" >> myScript.sh
echo "echo 'hello'" >> myScript.sh
echo "pwd" >> myScript.sh
If you open that file, you can find that it is simply a list of commands one at each line. Then, when you run the script, each command will be executed in order from top to bottom.
You can run the script using the following sintax:
./myScript.sh
Voilá!
crontab -e
Then add following line beside the above command:
30 10 * * * script.sh
It will run everyday at 10:30 AM
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have defined the path in .bash_profile file for /usr/share/totalscripts as:
PATH="${PATH}:/usr/share/totalscripts"
export PATH
Where all my shell scripts programs are present - /usr/share/totalscripts
Specifically, I need to execute the scripts in the current directory, not the one available in my $PATH environment variable. That is, executing a specific script rather than any script that matches that is found in the PATH.
For example, it might make sense to not call script1.sh (which will execute against any script1.sh found in the PATH), but instead call ./script1.sh (which will only execute the script that is found in the local directory).
This because I have the same script under /usr/share/totalscripts/script1.sh and /home/script1.sh (same names)
If you are working in a script, then you can define your environment variables at the start of it, like:
MY_VAR="/my/path/to/scripts"
And then call your script from within that script using:
${MY_VAR}/my_script.sh
Kind of the same as running the script using its full path.
I hope this helps!
Edit 1: - If you want to run a specific script as you mentioned in the comments, under a specific directory, you can try changing directories to the one that contains the file, and then running the script like ./script1.sh.
cd /path/containing/your/scripts/
./script1.sh
Edit 2: - Maybe you can create a wrapper for your script1.sh, placing the following if-block in there to check if the script is under the current directory or not.
if [ -e script1.sh ]; then
./script1.sh # runs the script under your current directory
else
script1.sh # runs the script searching for it in $PATH first
fi
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm attempting to execute the following series of commands to create backups of MySQL databases.
When I attempt to add the command to my crontab using crontab -e I get the error "errors in crontab file, cannot install" and asks me if I want to retry.
mkdir /home/mysql-backup/`date '+%m-%d-%Y'`; mysql -s -r -e 'show databases' | while read db; do mysqldump $db -r /home/mysql-backup/`date '+%m-%d-%Y'`/${db}.sql; done; rm -r -f `date --date="1 week ago" +%m-%d-%Y`; du -k |sort -n > output; mail -s "MySQL Backups" "steven#brightbear.net" < output
Is there anything I should be changing in this file? Or should I look into creating a script file and calling that from cron.
Thanks in advance for any assistance you can provide.
If you gave that script to crontab -e of course it will disagree. A line in a crontab file should start with 5 fields indicating when you want the script to run, as can be read in crontab's manpage.
On the other hand, most Linux distros nowadays have preset facilities for things that should be executed hourly (/etc/cron.hourly), daily (/etc/cron.daily), etc. It's a whole lot easier to just put your script in a file in the appropriate directory and it will get executed in the selected time raster. An added advantage is that in these files you won't be forced to cram everything into one line.
Yes; as a matter of style, if nothing else, I encourage to put the SQL commands into a shell script, and then run the shell script from cron. (And, as Anew points out, the command sequence is easier to maintain/debug if it’s broken out into multiple lines, with comments.) But –– is that all of what you’re feeding into crontab? Look at man crontab and add the fields that specify when you want the command to run.
From the crontab(5) man page, it looks like percent signs (%) have a special meaning, so that is probably where you're running into trouble.
Yes, you should put your commands into a separate shell script and just call that from the crontab line. This will also make it much easier to read the crontab file, and you can format your script nicely so that it's easier to maintain. And you can test it separately from the crontab that way.