Setting up crontab, still cant get it to work

OK I have been working on this simple action for a while and I cannot get it to work.
First off im new to the linux command line world. I feel like I am missing something simple.

What I am trying to achieve is that I want this command:

tcpdump -s2000 -w'flowroute-%H%M.pcap' -G900 -W36 &

to run at 0800 Monday through Friday.

I have setup a crontab that reads:

0 8 * * 1,2,3,4,5 /root/crontab.txt

the text file I'm using to run the command just says:

#captures packets in 15min segments and stops after 36 segments
#cronjob is setup for starting at 800 and then would go until 1700
tcpdump -s2000 -w'flowroute-%H%M.pcap' -G900 -W36

I have set the txt as an executable. Im running this from the root.
I have also try adding in an escape character breaks to compensate for the % symbol
what i mean:

tcpdump -s2000 -w'flowroute-\%H\%M.pcap' -G900 -W36

If any one has any suggestions or ideas for what i could do?
Currently this doesn't do any thing.

The crontab entry looks okay, but there's lots wrong with your script.

Technically the name doesn't matter but shell scripts are usually .sh, not .txt. It being named .txt makes me wonder if you set it executable -- if it's not, cron can't run it. chmod +x crontab.sh

The first line in any shell script should be a #! line telling it what shell to use, like this:

#!/bin/sh

Also, cron's path is pretty minimal, way more than a users', so might not know where to find tcpdump. You can [icode]. /etc/profile[icode] to read in a more usual profile before you try and run things.

You're missing a space between -w and the filename. You should also tell it exactly where to put them since cron may run things where you expect it to.

Having a script still running for hours on end may prevent root's crontab from running anything else during that time. If you put the command into the background, the shell will return immediately and cron won't have to wait for it. (If it hangs, cron will end up creating one more hanging tcpdump process per day, heh.)

So:

#!/bin/sh

. /etc/profile

# just to prove this script's running at all
touch /root/cronranme

#captures packets in 15min segments and stops after 36 segments
#cronjob is setup for starting at 800 and then would go until 1700
#
# Some shells don't have "disown", you can just lop that off if yours doesn't.
nohup tcpdump -s2000 -w '/root/flowroute-%H%M.pcap' -G900 -W36 & disown
1 Like

Thanks for the advice and help.

I have set the script
voippackets.sh ( i just renamed the file as such)
I double checked the permissions and they are set as an executable.

I rewrote the code as per your example

-bash-3.00# cat voippackets.sh
#!/bin/bash

#just to prove this script's running at all
touch /root/VoIPpackets

#capture packets in 15 min segments and stops after 36 segments

nohup tcpdump -s2000 -w '/root/flowroute-%H%M.pcap' -G900 -W36 & disown

I'm not sure what to put for the path and user. I use the root account. tcpdump is installed on the root. I also save files to the root as well.
What should I be using if not just
/root ?

Thanks for your help

Did it at least create /root/VoIPpackets ?

You omitted the most important line:

#!/bin/sh

. /etc/profile

# just to prove this script's running at all
touch /root/cronranme

#captures packets in 15min segments and stops after 36 segments
#cronjob is setup for starting at 800 and then would go until 1700
#
# Some shells don't have "disown", you can just lop that off if yours doesn't.
nohup tcpdump -s2000 -w '/root/flowroute-%H%M.pcap' -G900 -W36 & disown

I'm not sure what it means to put a path or put a user.

What do you mean by "installed on the root"? tcpdump is installed inside /root? If you've put tcpdump anywhere funny, you should run it with an absolute path like /path/to/tcpdump -s2000 ... Otherwise it will have no idea where to look for it.

You can save them wherever you want. The system can't stop you because you're root. Running as root may be unavoidable if you need to run tcpdump. In short, /root will work.

1 Like

Thanks for your reply,

i think im just a little confused.

I will add in

. /etc/profile

What does this line do?

Run all the lines in /etc/profile and include any variables from it into your shell. Unlike a shell login, crontab does not do this by default, you have to do it yourself.

Also, I repeat my question. Did it at least create /root/VoIPpackets ?

1 Like

I setup the new cron on friday and didnt do any testing on it yet. I will know more on Monday. Thanks for your help. I will let you know if it works for me or not :smiley:

If you don't know if it created /root/VoIPpackets, how do you know your cron script didn't work? :confused:

No luck again. The cron did not start at the time it was set too.

No it did not create /root/VoIPpackets

I know that the script didnt run because it should have generated a number a files. Those files were not created.

I have tested the command and it does work the way I want it to.
I have just run the script from the command line, it worked but i did need to put

bash voippackets

Im not sure if its is the script that i wrote that isnt working, because of lack of proper syntax.
or
If my crontab isn't set properly.

[icode]ls -l /root/scriptfile.sh please

If that works, it's definitely not named voippackets.sh Your crontab can't find it if you don't give it the actual name.

Also, if you need to run bash externally for it, it obviously isn't going to work in cron either, and likely isn't actually set executable.

chmod 755 /root/voippackets
ls -l /root/voippackets.sh

OutPut:

-rwxr-xr-x   1 root     root          238 Apr 15 16:15 /root/voippackets.sh

Sorry i miss typed on the earlier post

I ran the script as

bash voippackets.sh

BTW
my crontab reads

0 8 * * 1,2,3,4,5 /root/voippackets.sh

thanks for taking so much time to help me.

you could put 'bash /root/voippackets.sh' in your crontab I suppose...

---------- Post updated at 09:41 AM ---------- Previous update was at 09:32 AM ----------

to prove your cron is doing anything , at all, ever, without having to wait a whole week to tell, do this:

* * * * * touch /root/cronworks

Thanks for your help,

I can in today, I didnt come into the office on tuesday, I found that the changes that I made to the cron and the script on monday have made it work,
YAY!

Thank you so much for your help.

Im thinking it was this that I needed

. /etc/profile

Thank you so much!!!

1 Like

No problem. that's probably the most common cron problem.