Cron não Funciona! [RESOLVIDO]

1. Cron não Funciona! [RESOLVIDO]

Douglas dos Santos
error13

(usa Ubuntu)

Enviado em 24/06/2015 - 13:06h

Ola pessoal, já procurei não só fórum aqui mais também no pai google tem muitas coisas, porem ainda não consegui achar onde eu estou errando, vamos la:
Recentemente instalei server Debian ProFTPD para salvar as imagens das câmeras aqui da empresa, esta funcionando perfeitamente, logo percebi a necessidade de remanejar as imagens para melhor gerenciamento.
Então criei um script para coloca no cron, segue o script

#!/bin/bash 
#organização das imagens para melhor veficar.

#variavel do dia
hoje=$(date +"%Y%m%d")

#criação das pastas de backup
if [ ! -d "/home/camera/ftp/conjunto21/diarias/$hoje" ]; then
mkdir /home/camera/ftp/conjunto21/diarias/$hoje
fi
if [ ! -d "/home/camera/ftp/cpd/diarias/$hoje" ]; then
mkdir /home/camera/ftp/cpd/diarias/$hoje
fi
if [ ! -d "/home/camera/ftp/operacional/diarias/$hoje" ]; then
mkdir /home/camera/ftp/operacional/diarias/$hoje
fi
if [ ! -d "/home/camera/ftp/primeiro/diarias/$hoje" ]; then
mkdir /home/camera/ftp/primeiro/diarias/$hoje
fi

#mover os arquivos conjunto 21
cd /home/camera/ftp/conjunto21/

for arquivo in *.jpg; do
if [ "*.jpg" != ${arquivo[*]} ]; then
mv $arquivo diarias/$hoje/
fi
done

#mover os arquivos cpd
cd /home/camera/ftp/cpd/

for arquivo in *.jpg; do
if [ "*.jpg" != ${arquivo[*]} ]; then
mv $arquivo diarias/$hoje/
fi
done

#mover os arquivos operacional
cd /home/camera/ftp/operacional/

for arquivo in *.jpg; do
if [ "*.jpg" != ${arquivo[*]} ]; then
mv $arquivo diarias/$hoje/
fi
done

#mover os arquivos primeiro
cd /home/camera/ftp/primeiro/

for arquivo in *.jpg; do
if [ "*.jpg" != ${arquivo[*]} ]; then
mv $arquivo diarias/$hoje/
fi
done


executando ele na mão não gera nenhum erro efetua o que eu necessito, porem quando coloco no cron, não funciona!
alguem sabe me dizer o que pode ser?

segue como esta o cron (agora pq eu ja testei muitos jeitos e nenhum funciona)
# /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.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
45 12 * * * /sbin/.diarias.sh
#




  


2. MELHOR RESPOSTA

Wellingthon Dias Queiroz - @tonyhts
tonyhts

(usa Arch Linux)

Enviado em 25/06/2015 - 13:59h

error13 escreveu:

tonyhts escreveu:


Opa! Cheguei..

vamos lá...



E ai man desculpe a demora, mais essa sua sugestão cai na primeira opcao q coloquei do link aqui do forum mesmo, que inclusive o @C4rl tambem reforço, que o Cron nao funciona quando tem output, por isso colocamos essa logica no script do if, mais mesmo assim fiz o teste e segue o resultado.
root@srv-ftp:/script# cat teste_diarias_error.log 
mv: impossível obter estado de “*.jpg”: Arquivo ou diretório não encontrado






Humm.. vdd

aqui rolou assim (( colando um ls *.jpg - em acentos)):


for arquivo in `ls *.jpg`; do
# if [ "*.jpg" != ${arquivo[*]} ]; then
mv $arquivo diarias/$hoje/
#fi
done


EDIT::

coloca no cron assim:

20 14 * * * root bash /script/diariasXX.sh > /script/final.log 2>&1 


OBS:: corrige o nome arquivo

3. Re: Cron não Funciona! [RESOLVIDO]

Wellingthon Dias Queiroz - @tonyhts
tonyhts

(usa Arch Linux)

Enviado em 24/06/2015 - 13:25h


olá,
o nome do seu script tem o ponto antes mesmo ?


4. Re: Cron não Funciona! [RESOLVIDO]

Douglas dos Santos
error13

(usa Ubuntu)

Enviado em 24/06/2015 - 13:32h

tonyhts escreveu:


olá,
o nome do seu script tem o ponto antes mesmo ?


E ai @tonyhts seguindo as instruções desse link aki da comunidade
http://www.vivaolinux.com.br/topico/Ubuntu-e-Kubuntu/crontab-X-script.sh-%5BRESOLVIDO%5D

o @maran diz no fim do posto sobre colocar no /sbin como um ponto!
só segui pra ver se funcionava porem nao funciona com o ponto e nem sem o ponto! rs

segue tabem as permissoes do arquivo;
root@servidor:/sbin# ls -la | grep .diaras.sh 
-rwxr-xr-x 1 root root 1254 Jun 24 12:31 .diaras.sh






5. Re: Cron não Funciona!

Wellingthon Dias Queiroz - @tonyhts
tonyhts

(usa Arch Linux)

Enviado em 24/06/2015 - 13:38h

Saquei!

Acredito que ja deva ter tentado , mas não custa perguntar rs:

ja tentou mandar em uma pasta que não seja sbin ? tipo /home/user/diarias.sh ?

tentou adicionado o user?

juntando as duas peguntas, tentou assim? :

45 12 * * *  root /home/user/diarias.sh 



6. Re: Cron não Funciona!

Douglas dos Santos
error13

(usa Ubuntu)

Enviado em 24/06/2015 - 13:41h

tonyhts escreveu:

Saquei!

Acredito que ja deva ter tentado , mas não custa perguntar rs:

ja tentou mandar em uma pasta que não seja sbin ? tipo /home/user/diarias.sh ?

tentou adicionado o user?

juntando as duas peguntas, tentou assim? :

45 12 * * *  root /home/user/diarias.sh 


E ai man nao q eu nao tenha tentado antes rs...
mais fui mais rapido q você e tentei assim
root@srv-ftp:/sbin# cat /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.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
45 13 * * * root /sbin/.diarias.sh
#
root@srv-ftp:/sbin# chmod +x .diaras.sh
root@srv-ftp:/sbin# ls -la | grep .diaras.sh
-rwxr-xr-x 1 root root


e reinicei, porque foi a unica coisa q ainda nao tinha tentado!
Mais nao funcionou!



7. Re: Cron não Funciona!

Wellingthon Dias Queiroz - @tonyhts
tonyhts

(usa Arch Linux)

Enviado em 24/06/2015 - 13:53h

Muito estranho... e agora estou mega curioso. rsrs

vamos lá

tenta isso:

cria um script teste:

#!/bin/bash
# teste cron/script

ls /var > /var/log/teste.log

exit 1


da um chmod +x nele e manda ele no cron.
depois ve se gerou o teste.log






8. Re: Cron não Funciona! [RESOLVIDO]

Douglas dos Santos
error13

(usa Ubuntu)

Enviado em 24/06/2015 - 13:57h

tonyhts escreveu:

Muito estranho... e agora estou mega curioso. rsrs

vamos lá

tenta isso:

cria um script teste:

#!bin/bash
# teste cron/script

ls /var > /var/log/teste.log

exit 1


da um chmod +x nele e manda ele no cron.
depois ve se gerou o teste.log





Tambem to a um tempao!
mais só uma dubida essse "exit 1" e pra ter mesmo?





9. Re: Cron não Funciona! [RESOLVIDO]

Wellingthon Dias Queiroz - @tonyhts
tonyhts

(usa Arch Linux)

Enviado em 24/06/2015 - 13:58h


tanto faz, é mania minha!

OBS:: o #!/bin/bash tava errado, eu arrumei lá (estava sem o primeiro / )


10. Re: Cron não Funciona!

Douglas dos Santos
error13

(usa Ubuntu)

Enviado em 24/06/2015 - 14:15h

e @tonyhts e ai campeão segue o resultado:

ps.funciono rs.

root@srv-ftp:/sbin# vim testecron.sh
root@srv-ftp:/sbin# chmod +x testecron.sh
root@srv-ftp:/sbin# ./testecron.sh
root@srv-ftp:/sbin# cat testecron.sh
#!/bin/bash

# teste cron/script

ls /var > /var/log/teste.log

root@srv-ftp:/sbin# ls -la /var/lo
local/ lock/ log/ lost+found/
root@srv-ftp:/sbin# ls -la /var/log/ | grep testecron
root@srv-ftp:/sbin# ls -la /var/log/ | grep teste
-rw-r--r-- 1 root root 67 Jun 24 14:03 teste.log
root@srv-ftp:/sbin# rm -r /^C
root@srv-ftp:/sbin#
root@srv-ftp:/sbin# rm -r /var/log/teste.log
root@srv-ftp:/sbin# cat /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.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
15 14 * * * root /sbin/testecron.sh
#45 13 * * * root /sbin/.diarias.sh
#
root@srv-ftp:/sbin#
root@srv-ftp:/sbin# crontab /etc/crontab
root@srv-ftp:/sbin# /etc/init.d/cron restart
[ ok ] Restarting cron (via systemctl): cron.service.
root@srv-ftp:/sbin#
root@srv-ftp:/sbin# date
Qua Jun 24 14:09:47 BRT 2015
root@srv-ftp:/sbin#
root@srv-ftp:/sbin# rm -r /var/log/teste.log
rm: não foi possível remover "/var/log/teste.log": Arquivo ou diretório não encontrado
root@srv-ftp:/sbin# date
Qua Jun 24 14:11:00 BRT 2015
root@srv-ftp:/sbin#
root@srv-ftp:/sbin# date
Qua Jun 24 14:15:12 BRT 2015
root@srv-ftp:/sbin# ls -la /var/log/ | grep teste
-rw-r--r-- 1 root root 67 Jun 24 14:15 teste.log
root@srv-ftp:/sbin#





11. Re: Cron não Funciona!

Wellingthon Dias Queiroz - @tonyhts
tonyhts

(usa Arch Linux)

Enviado em 24/06/2015 - 14:20h

Huhumm.. então pra matarmos de vez, vamos testyar seu script :

manda assim no cron

45 13 * * * root /sbin/.diarias.sh 2> /var/teste_diarias_error.log 


ja não lembro se agora o seu script ta com ou sem ponto.. rs mas verifica ai...

vamos ver o resultado:

PS.: Pipoca na mão , ansiedade no talo! rs


12. Re: Cron não Funciona! [RESOLVIDO]

Douglas dos Santos
error13

(usa Ubuntu)

Enviado em 24/06/2015 - 14:28h

tonyhts escreveu:

PS.: Pipoca na mão , ansiedade no talo! rs


e ai man!!! funciono!
olha o log:
root@srv-ftp:/sbin# ls /var/
backups cache lib local lock log lost+found mail opt run spool teste_diarias_error.log tmp
root@srv-ftp:/sbin# cat /var/teste_diarias_error.log
/bin/sh: 1: /sbin/.diarias.sh: not found


NOTFOUND ????







  



Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts