Find the answer to your Linux question:
Results 1 to 6 of 6
I've got some problem. Hope you can help me. First my script: Code: #/bin/bash /usr/bin/clear logs=$(date +"%Y-%m-%d %H:%M") echo echo "####################################################################" echo "# Changing the password on the server $logs ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4

    Bash + Crontab + If-statement seems to give some problems

    I've got some problem. Hope you can help me.

    First my script:
    Code:
    #/bin/bash
    
    /usr/bin/clear
    
    logs=$(date +"%Y-%m-%d %H:%M")
    
    echo
    echo "####################################################################"
    echo "# Changing the password on the server $logs             #"
    echo "####################################################################"
    
    MAXCOUNT=2
    count=1
    while [ "$count" -le $MAXCOUNT ]
    do
      number=$RANDOM
      password+=$number
      let "count += 1"
    done
    echo
    echo $password | passwd --stdin movies
    echo
    echo "Username: movies"
    echo "Password: $password"
    echo
    echo "The password for user movies successful changed in Linux-database!"
    echo "\\\\\\--------------------------------------------------------------///"
    echo
    echo
    
    logs="$logs \t x"
    
    echo "####################################################################"
    echo "# Changing the password on the website                             #"
    echo "####################################################################"
    DayOfWeek=$(date +"%u")
    
    if [ $DayOfWeek = 1 ] || [ $DayOfWeek = 2 ] || [ $DayOfWeek = 5 ] || [ $DayOfWeek = 6 ]
    then
            echo "FTP-info:<br /><br /><strong>Status:</strong> Online<br /><strong>Username:</strong> movies<br /><strong>Password:</strong> $password" > website/httpdocs/password.html
            echo
            echo "The password for user movies successful changed on the website!"
            echo "Users can see the login and the password."
            logs="$logs \t\t x \t x"
    else
            echo "FTP-info:<br /><br /><strong>Status:</strong> Online<br /><strong>Username:</strong> *<br /><strong>Password:</strong> *" > website/httpdocs/password.html
            echo
            echo "The password for user movies successful changed on the website!"
            echo "The login and the password is invisible for the users."
            logs="$logs \t\t x \t "
    fi
    echo
    echo "\\\\\\--------------------------------------------------------------///"
    echo
    echo
    
    echo "####################################################################"
    echo "# Block accounts                                                   #"
    echo "####################################################################"
    
    DayOfMonth=$(date +"%d")
    Hours=$(date  +"%H")
    
    if [ $DayOfMonth = 10 ] && [ $Hours = 23 ]
    then
            echo "10 days account must be blocked"
            /usr/sbin/usermod -L 10days
            echo "10 days account is blocked now"
            logs="$logs \t\t\t x"
    else
            echo "10 days account mustn't be blocked"
            logs="$logs \t\t\t  "
    fi
    echo
    echo "\\\\\\--------------------------------------------------------------///"
    echo
    echo
    
    echo "########################################################################################################"
    echo "# Logs                                                                                                 #"
    echo "########################################################################################################"
    echo "|                                                                                                      |"
    echo -e "|\tDate \t\t\tDatabase \tWebsite \tVisible \tBlocked                |"
    echo -e "|\t$logs                     |"
    echo "|                                                                                                      |"
    echo "\\\\\\--------------------------------------------------------------------------------------------------///"
    
    echo -e $logs >> /root/PasswordChanging.log
    What this script must do:
    I have my own ftp-server. The password on that ftp must be changed automaticly. No problem with that.

    The password of the script must be shown on the website on monday, tuesday, friday and saturday.

    So I've made that script. Tested it on my server like: bash ~/PasswordChanger.sh

    It works fine!

    After, I added this task into my Crontab and got problems with the if-statement. It isn't working. May be some of you guys can prupose something to solve the problem?

    Thanks in advance. Hope you realy can help me.

  2. #2
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Hi

    What's theif error you get?
    Can you see anything in the cron log (var/log/cron)?
    One Love!!!

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4
    Quote Originally Posted by goude View Post
    Can you see anything in the cron log (var/log/cron)?
    No. There is nothing in that log-file.

    The script is working strange. Now it works for changing password and to give it back to the website. So the if-statement works fine, I think.

    What did I do next? I've inserted the next part in my script in few places:
    Code:
    echo -e "01: \n $logs" >> test.txt
    
    ...
    
    echo -e "05: \n $logs" >> test.txt
    I thought if I do it like that I'll se where the script goes wrong. This echo worked until the first if-statement. That's why i thought the if-statement doesn't works in crontab.

    What i see now: Tha password is changing and is displayed on the website. So the if-statement is not the problem. But I can't write the log-file bu using that command:
    Code:
    echo -e $logs >> /root/PasswordChanging.log
    May be is it difficult to understand what I mean, but my English is not so good, that's why I can't explain my problem flewent. Hope you understand me and can help me.

  4. #4
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Don't worry about your English.

    Did you try running the script and redirecting the output manually (
    script.sh >> /root/PasswordChanging.log)
    ?

    See if that works.

    To be honest I'm not very familiar with bash so I hope someone else can detect any errors in your script.
    One Love!!!

  5. #5
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4
    Quote Originally Posted by goude View Post
    Did you try running the script and redirecting the output manually (
    script.sh >> /root/PasswordChanging.log)
    ?
    Oh sorry. I forgot to mention it... I have no problems running the script manualy. Everythig works good. I mean there are no problems with If's ect. Also whriting the log file works fine. That's why I have to find someone who can help me to find an answer.

    It's strange, I can understand it, but may be someone already had the same problem and can give me the solution.

  6. #6
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4
    Anyone who can help me?

    I still have that problem. To be clear:
    I've made a bashscript. I've used few if-statements in that script. If i run it self using "bash script-name.sh" it works fine. Then I've inserted the command into the crontab. It works until it reachs the first if-statement.

    May be I have to load something into my script so that it works fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...