I'm running a script from crontab in which I want to set the symbolic link for npx. It does some other things which are dependent on the npx command itself. Its running the script as expected on the giving time interval, but its giving me no result for command which npx or whereis npx. When I try to run the script from terminal directly these commands does generate the correct path.
Note that, crontab I'm using is under the root user privilege, i.e set with sudo crontab -e and verified with echoing whoami inside the script which generate 'root')
By default, crontab will run your cron jobs using sh which might be the reason you are getting no results.
Try to explicitly change the shell to your default shell by adjusting the crontab entry:
*/30 * * * * /bin/bash -c "/my_script.sh"
In this case I changed it to bash, you can change it to your desired shell.
i wrote a sh file which kills a tomcat process, suppose my shell scrip is in /usr/temp, script looks likethat:
#!/bin/bash
ps -Af | grep "tomcat_something" | grep -v grep | awk '{print$2}' | xargs sudo kill -9
then i created a cron via crontab -e My cron looks likethat (dont pay attention to the time):
10 10 * * * /usr/temp/myshellscript.sh
If i run the sh without cron it works fine, but via cron is not working at all.
I even tryied to run the same shell script with a touch file command and in this case cron works fine, so my doubt is that the problem is the kill command. Can someone help ?
Problem:
sudo in cron job does not work as it expects a password to be entered.
Possible Solutions
Not Recommended as require to give password in plaintext.
Use expect (Quick google or SO search will help you) in your script
and provide sudo's password there.
Run your script as sudo instead of running sudo inside script
In your own crontab, write your command like so:
10 10 * * * echo "your password" | sudo -S /usr/temp/myshellscript.sh
Again, it is not recommended
Best Possible Way: Use roots crontab (only if you have access)
Run the following command:
sudo crontab -e
This opens up root's crontab. sudo is not necessary to run your command in this context since it'll be invoked as root anyway.
I've accidentally ran 'crontab *'. Afterwards, crontab -l and, possibly, crontab -e stopped working properly.
Can someone more knowledgeable in cron help me explain what would happen if 'crontab *' command is ran?
I ran crontab -l | grep * and very few cronjobs showed up. I also ran crontab -e in order to edit the crontab file and it gives me the "Error detected while procedssing 'pathway'" message and "E518: Unknown option: foldmehod=marker" error. It brings me to /tmp/crontab* where * denotes an attempt to create a cron file at tmp folder.
I expect the output of crontab -l | grep "name" to output something but the output doesn't show anything. I suspect it's me running crontab *.
In short, the result would be crontab attempting to install every file that * would have expanded to. Since you did not specify a user with -u first, it would have defaulted to whatever user you were executing the command as. The good news is this only messed up your private crontab.
Cron uses two different crontabs; a system-wide crontab at /etc/crontab and the private, user-specific crontabs at /var/spool/cron. When you use crontab, for example as root, to install a new file, those changes are actually made to /var/spool/cron/root, not the global crontab found at /etc/crontab.
Thus the damage is mitigated to the private crontab of whichever user ran that command, which you can most likely safely remove and rebuild if necessary; but whatever is inside of that private crontab is going to be junk.
I have a script that creates some temporary files that need to remain in place for quite some time. I want the user who executes the script to be able to create their own custom crontab that removes these files at a later time.
To test, I've just simply tried to setup a simple crontab using the command-line exclusively, but I'm not sure if this is even possible.
From the command-line I type the following:
$ crontab 1 * * * * $(mkdir -p ~/Desktop/CronSuccess)
I get the error: crontab: 1: No such file or directory
Is there anyway to have a script create a fully functional crontab on the fly?
Read the manpage for crontab
You can't do what you're trying to do.
You can however have crontab read the entries from a file.
e.g.
echo 'crontab 1 * * * * mkdir -p ~/Desktop/CronSuccess' >mycrontab
crontab mycrontab
Be aware that this will not append to the users crontab, it will replace the existing crontab of the user
with what's in the mycrontab file.
Surrounding the mkdir command with $() would be wrong.
You might also use at if it suits your needs:
e.g.
echo 'mkdir -p ~/Desktop/CronSuccess' | at now + 10 hours
This question already has answers here:
CronJob not running
(19 answers)
Closed last month.
I have a cron job that I want to execute every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
In /var/spool/cron/crontabs/root
The cron should execute a shell script:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
That works fine from command line but not from cron. However the cron itself is startet but the script does not start.
I read about the path problem but I dont really understand it. I setup a cron that writes some env data to a file. This is the output:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
If I execute the env command in command line I get following output for PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
What path do I have to set in my shell script?
Your $PATH is fine; leave it alone. On Ubuntu, all the commands you're invoking (touch, chmod, curl) are in /bin and/or /usr/bin.
How did you set up the cron job? Did you run crontab some-file as root?
It seems that /etc/crontab is the usual mechanism for running cron commands as root. On my Ubuntu system, sudo crontab -l says no crontab for root. Running crontab as root, as you would for any non-root account, should be ok, but you might consider using /etc/crontab instead. Note that it uses a different syntax than an ordinary crontab, as explained in the comments at the top of /etc/crontab:
$ head -5 /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Run sudo crontab -l. Does it show your command?
Temporarily modify your script so it always produces some visible output. For example, add the following right after the #!/bin/sh:
echo "Running scheduleSpider.sh at \`date\`" >> /tmp/scheduleSpider.sh.log
and see what's in /tmp/scheduleSpider.sh.log after a few minutes. (You can set the command to run every minute so you don't have to wait as long for results.) If that works (it should), you can add more echo commands to your script to see in detail what it's doing.
It looks like your script is designed to run only once; it creates the sync.txt file to prevent it from running again. That could be the root (ahem) of your problem. What that your intent? Did you mean to delete sync.txt after running the command, and just forgot to do it?
root's home directory on Ubuntu is /root. The first time your script runs, it should create /root/sync.txt. Does that file exist? If so, how old is it?
Note that curl someLink (assuming someLink is a valid URL) will just dump the content from the specified link to standard output. Was that your intent (it will show up as e-mail to root? Or did you just not show us the entire command?
First: you can substitute the first field with */5 (see man 5 crontab)
Second: have cron mail the output to your email address by entering MAILTO=your#email.address in your crontab. If the script has any output, it'll be mailed. Instead of that, you may have a local mailbox in which you can find the cron output (usually $MAIL).
A better syntax for you CRON is
*/5 * * * * /scr_temp/scheduleSpider.sh
Also, check the authority of your scheduleSpider.sh file. Cron runs under a different user than the one you are likely executing your program interactively, so it may be that cron does not have authority. Try chmod 777 for now, just to check.
I suggest to:
check that /scr_temp/scheduleSpider.sh has executable bit
set PATH properly inside your script or use absolute path to command (/bin/touch instead of touch)
specify absolute path to sync.txt file (or calculate it relatively to script)
Have you added the comand via crontab -e or just by editing the crontab file? You should use crontab -e to get it correctly updated.
Set the working directory in the cron script, it probably doesn't execute the things where you think it should.
You should add /bin/sh before the absolute path of your script.
*/5 * * * * /bin/sh /scr_temp/scheduleSpider.sh