I created a shell script that will move and copy some files. My script is working. Everyday I run it manually because cron is not doing its job (I probably did it wrong). My shell file has permissions 777, all folders, CRON/ROOT is have permissions 777. I'm running Cent OS 6.7 Final. I want to run this cron everyday at 01:01AM.
My script (pdb.sh)
#!/bin/bash
/etc/init.d/pdb stop
mv /var/www/html/acesso/ui/root/mtaserver.conf /root/PDB/mods/deathmatch/mtaserver.conf
sleep 2
cp /root/PDB/mods/deathmatch/mtaserver.conf /var/www/html/acesso/ui/root/mtaserver.conf
sleep 2
mv /var/www/html/acesso/ui/root/admintag.lua /root/PDB/mods/deathmatch/resources/[RESOURCES]/022/admintag.lua
sleep 2
cp /root/PDB/mods/deathmatch/resources/[RESOURCES]/022/admintag.lua /var/www/html/acesso/ui/root/admintag.lua
sleep 2
chmod -R 777 /var/www/html/acesso/ui/root/
/etc/init.d/pdb start
crontab -l
crontab -e
Try putting /bin/sh in front of the script location. – Matt Schuchard
Related
I'm trying to run cron job in azure cloud shell
but it is not working
This is my simple cron job
* * * * * /home/meet/clouddrive/temp.sh
where
cat /home/meet/clouddrive/temp.sh
#!/bin/bash
echo "meet" >> /home/meet/clouddrive/test.txt
pwd
/home/meet/clouddrive
meet [ ~/clouddrive ]$ ls
temp.sh
The script is located at /home/meet/clouddrive/temp.sh. However, the pwd command in the script outputs /home/meet/clouddrive, which suggests that the script is not in the current working directory. To fix this, you can either adjust the path to the script in the cron job, or you can change the current working directory in the script to the correct location.
Make sure that the script has the appropriate permissions to be executed. You can check the permissions for the script using the ls -l command, and you can change the permissions using the chmod command. For example, to make the script executable by the owner, you can run chmod u+x /home/meet/clouddrive/temp.sh.
Can you try running the script manually from the command line to see if it works fine?
I tried to reproduce the same in my environment and got the results like below:
I tried to run Cron job in azure cloud shell like below:
when I try crontab -l there is no job from here.
Then create crontab -e
Run */1 * * * * echo "this is a test" /home/imran123/testfile.txt
Then try to create a file using vi testfile.txt and I mention "This is test"
Then try to give execute permission like below:
chmod +x test.sh
Then when I executed cmd it run successfully like below:
crontab -l
cat testfile.txt
I tried to configure crontab to execute a shell script every day.
When executed manually, the file works well. Unfortunately, crontab won't execute it.
Here's my shell file:
#! bin/bash
# GENERAL properties
BASE_DIR=/opt/XXX-1.0
# JOB properties
JOBS_DIR=$BASE_DIR/jobs
#find all main etl jobs and execute them
cd $JOBS_DIR
find . -name '*mainrun.sh' -exec {} \;
And here's my crontab
10 14 * * * /bin/sh /opt/XXX-1.0/jobs/jobs.sh
Any ideas on what could be preventing me from executing it?
Thank you.
I haven't seen a /bin/sh in a crontab like that.
Why aren't you using a shebang at the start of your file like so:
#!/usr/bin/env bash
Is the file itself executable for the crontab user that is executing it?
chmod +x /opt/XXX-1.0/jobs/jobs.sh
I'm trying to add a cron job on a shared hosting like this
/usr/bin/php /home/USER/public_html/LARAVEL_PROJECT/artisan schedule:run >> /dev/null 2>&1
The support confirmed the path and command is correct but they don't allow special characters in the command so I have to remove >> /dev/null 2>&1 and that doesn't make it work. Any work around?
You can create own shell script, add there the entire command (with redirection) and ask to add this shell file in to the cron
You can create the script to be: /home/USER/script.sh and to contain
#!/bin/bash
/usr/bin/php /home/USER/public_html/LARAVEL_PROJECT/artisan schedule:run >> /dev/null 2>&1
Then you need to make it executable (via ssh for example)
chmod +x /home/USER/script.sh
or
chmod 750 /home/USER/script.sh
And then ask support to run this script instead of your line.
Currently I am writing a little script which should add a cronjob to the root crontable. But it seems that my root crontable stopped working. When I try to run the crontab commands in my bash scrip, I get "command not found". Also it worked for some time and stopped working yesterday. Now when I enter "sudo crontab -l" I don't get "no crontab for root" anymore. I am not sure what I did wrong. Here is my code:
#!/bin/bash
sudo crontab -l > rootcron 2> /dev/null
sudo echo "test" >> rootcron
sudo crontab rootcron
sudo rm rootcron
You didn't specify when the command is to be run. Typically you would see something like:
*/5 * * * * touch /tmp/test-cron
So basically you probably have an invalid cron file. What are the contents of the file now?
I am trying to run a shell script on crontab on Ubuntu platform. I have tried googling and other links but nothing has helped so far.
This is my crontab:
*/2 * * * * sudo bash /data/html/mysite/site_cleanup.sh
This is the content of my sh file:
#!/bin/sh
# How many days retention do we want ?
DAYS=0
# geting present day
now=$(date +"%m_%d_%Y")
# Where is the base directory
BASEDIR=/data/html/mysite
#where is the backup directory
BKPDIR=/data/html/backup
# Where is the log file
LOGFILE=$BKPDIR/log/mysite.log
# add to tar
tar -cvzf $now.tar.gz $BASEDIR
mv $now.tar.gz $BKPDIR
# REMOVE OLD FILES
echo `date` Purge Started >> $LOGFILE
find $BASEDIR -mtime +$DAYS | xargs rm
echo `date` Purge Completed >> $LOGFILE
The same script runs from a terminal and gives the desired result.
Generic troubleshooting for noninteractive shell scripts
Put set -x; exec 2>/path/to/logfile at the top of your script to log all subsequent commands to a file as they're run. If this doesn't work, you'll know that your script isn't being run at all; if it does, you'll know where it fails and how.
If this is a personal crontab
If you're running crontab -e as a user (without sudo), then the crontab being modified is one for commands run with that user's permissions. Check that file permissions allow that user to modify the content in question (which, if these files are in a cgi-bin directory, may require being run by the same user as the web server).
If your intent is to have commands run as root, rather than as your own user, be sure you use sudo when editing the crontab to edit the system crontab instead (but please take care as to your script's correctness in this case -- carelessness such as missing quotes or lack of appropriate precautions in xargs usage can cause a script to delete the wrong files if malicious filenames are created):
sudo crontab -e ## to edit the system (root) crontab
...or, if you're cleaning up files owned by the apache user (for example; check which account is correct for your own operating system and web server):
sudo -u apache crontab -e ## to edit the apache user's crontab
Troubleshooting for a system crontab
Do not attempt to put a sudo command within the commands run by cron; with sudo's default configuration, it requires a TTY (a keyboard and screen) to be attached to a session in order to run. Thus, your crontab line should not contain sudo, but instead should look like the following:
*/2 * * * * bash /data/html/mysite/site_cleanup.sh
Your issue is likely coming from the sudo call from your user level cron. Unless you've gone through and edited the bashrc profile to allow that script to run without sudo it'll hang up every time.
So you can lookup how to run a script with no password by modifying the bashrc profile, remove the sudo call if you aren't doing something in your script that calls for Super User permissions, or as a last ditch, extremely bad idea you can call your script from root's cron by doing sudo crontab -e or sudo env EDITOR=nano crontab -e if you prefer nano as your editor.
try to add this line to the crontab of user root and without the sudo.
like this:
*/2 * * * * bash /data/html/mysite/site_cleanup.sh