bash from cron / bash to sh convert

Dear colleagues,
once again i need a little help.

I am using quite complex Squid & DansGuardian setup where i authenticate users from Active Directory over NTLM, and use multiple groups with different levels of web access. Everything works great and i would be glad to share my setup with anyone who is interested.

However, i have a little problem. I am using following script:
http://dansguardian.org/downloads/agawronski/usermap2
to pull users from Active Directory and map them to appropriate access groups in DansGuardian. The script (after few path modifications and chmodding to 777) works fine if i run it from command line like:
[CMD="rsbgyucsquid05# "]/usr/local/bin/bash /usr/local/etc/dansguardian/usermap2[/CMD]
but if run from cron, as root:
[CMD=""]@daily /usr/local/bin/bash /usr/local/etc/dansguardian/usermap2 >/dev/null 2>&1[/CMD]
it creates empty file.

So, i would like to make it work as a cron job, preferrably by converting it to sh script. If someone can do this for me i would be very thankful. I know i deserve an answer such as 'do it yourself' as i would be able to rewrite it myself after a bit of reading. I need it quite fast however.

Second solution would be if someone advised me how to run bash scripts from cron.

Thank you in advance,
 
Change the first line of the script to:
Code:
#!/usr/local/bin/bash
And set the execute bit.

Also remove the 777 from that file! This will make it world-writable, meaning anyone and everyone can modify that file. Certainly not something you want when it's run as root.
 
Hey SirDice,

thank you for your quick reply. I didn't really chmod it to 777, just wrote it that way so people do not instruct me about execute bit :)

I also tried to change the first line to point to actual bash executable, but it didn't work. As i read, cron uses sh by default. Now, when i try to execute the script as cron would, i get following error:
[CMD=""]rsbgyucsquid05# sh usermap2
dgusermaps: 42: Syntax error: word unexpected (expecting ")")
[/CMD]
Line 42 declares groups in AD which should be queried:
Code:
# Declare array with all security groups you want queried. Add as many as you want.
group=( 'Proxy_Banned' 'Proxy_Basic' 'Proxy_Advanced' )
I think problem is related to different syntax in declaring arrays in sh and bash.

Any other advices?
 
pacija said:
I also tried to change the first line to point to actual bash executable, but it didn't work. As i read, cron uses sh by default.
Just execute the file, forget about sh or bash. The shebang in the first line will make sure it will use bash, that's what it's supposed to do. Cron is quite capable of executing just about anything. If the script is executable just put it in there like you would any kind of executable.
 
Make sure you have Bash installed, and that the #! line in the scripts uses /usr/local/bin/bash and not /bin/bash.

Next, make sure you specify full paths to all commands inside of that script. Some things will be located in different places compared to a Linux system. And cron runs with a very limited PATH (/usr/local/* is not in it).

Finally, remove the ">/dev/null 2>&1" from the cronjob entry so that you get a list of any errors that occur. Only once the script is working correctly should you put that in.
 
Back
Top